home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_11.gml < prev    next >
Text File  |  1998-03-03  |  143KB  |  3,177 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. B<br>
  7. <b>bad</b> member function 
  8.  
  9. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  10. <br>
  11. <b>badbit</b> 
  12. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  13. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  14. <a href="%s8p2"><img src=iicons/bullbib.gif></a>
  15. <br>
  16. C<br>
  17. <b>cerr</b> 
  18. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  19. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  20. <br>
  21. <b>cin</b> 
  22. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  23. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  24. <br>
  25. <b>clear</b> member function 
  26.  
  27. <a href="%s8p3"><img src=iicons/bullbib.gif></a>
  28. <br>
  29. <b>clog</b> 
  30. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  31. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  32. <br>
  33. <b>cout</b> 
  34. <a href="%s2p5"><img src=iicons/bullbib.gif></a>
  35. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  36. <a href="%s3p5"><img src=iicons/bullbib.gif></a>
  37. <br>
  38. D<br>
  39. <b>dec</b> 
  40. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  41. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  42. <a href="%s7p11"><img src=iicons/bullbib.gif></a>
  43. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  44. <br>
  45. E<br>
  46. </font>
  47.  
  48. </page>
  49. <page>
  50. <font size=14>
  51. end-of-file 
  52. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  53. <br>
  54. <b>eof</b> member function 
  55. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  56. <br>
  57. <b>eofbit</b> 
  58. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  59. <a href="%s8p2"><img src=iicons/bullbib.gif></a>
  60. <br>
  61. extensibility 
  62. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  63. <br>
  64. F<br>
  65. <b>fail</b> member function 
  66.  
  67. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  68. <br>
  69. <b>failbit</b> 
  70. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  71. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  72. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  73. <a href="%s8p2"><img src=iicons/bullbib.gif></a>
  74. <br>
  75. field width 
  76. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  77. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  78. <a href="%s6p6"><img src=iicons/bullbib.gif></a>
  79. <br>
  80. fill character 
  81. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  82. <a href="%s6p6"><img src=iicons/bullbib.gif></a>
  83. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  84.  
  85. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  86. <br>
  87. <b>fill</b> member function 
  88.  
  89. <a href="%s7p6"><img src=iicons/bullbib.gif></a>
  90. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  91. <br>
  92. </font>
  93.  
  94. </page>
  95. <page>
  96. <font size=14>
  97. <b>flags</b> member function 
  98.  
  99. <a href="%s7p17"><img src=iicons/bullbib.gif></a>
  100. <br>
  101. <b>flush</b> 
  102. <a href="%s3p3"><img src=iicons/bullbib.gif></a>
  103. <br>
  104. format flags 
  105. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  106. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  107. <a href="%s7p17"><img src=iicons/bullbib.gif></a>
  108.  
  109. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  110. <br>
  111. format state 
  112. <a href="%s7p17"><img src=iicons/bullbib.gif></a>
  113. <br>
  114. formatted input/output 
  115.  
  116. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  117. <br>
  118. <b>fstream</b> 
  119. <a href="%s2p12"><img src=iicons/bullbib.gif></a>
  120. <br>
  121. G<br>
  122. <b>gcount</b> 
  123. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  124. <br>
  125. <b>get</b> member function 
  126. <a href="%s4p5"><img src=iicons/bullbib.gif></a>
  127.  
  128. <a href="%s4p7"><img src=iicons/bullbib.gif></a>
  129. <br>
  130. <b>getline</b> 
  131. <a href="%s4p8"><img src=iicons/bullbib.gif></a>
  132. <br>
  133. </font>
  134.  
  135. </page>
  136. <page>
  137. <font size=14>
  138. <b>good</b> member function 
  139.  
  140. <a href="%s8p2"><img src=iicons/bullbib.gif></a>
  141. <br>
  142. H<br>
  143. <b>hex</b> 
  144. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  145. <a href="%s7p11"><img src=iicons/bullbib.gif></a>
  146. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  147. <br>
  148. I<br>
  149. <b>ifstream</b> 
  150. <a href="%s2p12"><img src=iicons/bullbib.gif></a>
  151. <br>
  152. <b>ignore</b> 
  153. <a href="%s4p11"><img src=iicons/bullbib.gif></a>
  154. <br>
  155. <b>iomanip.h</b> 
  156. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  157. <br>
  158. <b>ios</b> base class 
  159. <a href="%s2p8"><img src=iicons/bullbib.gif></a>
  160. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  161. <br>
  162. <b>ios::adjustfield</b> 
  163. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  164.  
  165. <a href="%s7p11"><img src=iicons/bullbib.gif></a>
  166. <a href="%s7p13"><img src=iicons/bullbib.gif></a>
  167. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  168. <a href="%s7p19"><img src=iicons/bullbib.gif></a>
  169. <br>
  170. <b>ios::basefield</b> 
  171. <a href="%s7p13"><img src=iicons/bullbib.gif></a>
  172. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  173. <br>
  174. <b>ios::fixed</b> 
  175. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  176. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  177. <a href="%s7p19"><img src=iicons/bullbib.gif></a>
  178. <br>
  179. </font>
  180.  
  181. </page>
  182. <page>
  183. <font size=14>
  184. <b>ios::floatfield</b> 
  185. <a href="%s7p13"><img src=iicons/bullbib.gif></a>
  186. <a href="%s7p14"><img src=iicons/bullbib.gif></a>
  187.  
  188. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  189. <br>
  190. <b>ios::internal</b> 
  191. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  192. <a href="%s7p19"><img src=iicons/bullbib.gif></a>
  193. <br>
  194. <b>ios::scientific</b> 
  195. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  196. <a href="%s7p19"><img src=iicons/bullbib.gif></a>
  197. <br>
  198. <b>ios::scientific</b> flag 
  199. <a href="%s7p13"><img src=iicons/bullbib.gif></a>
  200. <br>
  201. <b>ios::showbase</b> 
  202. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  203. <br>
  204. <b>ios::showpoint</b> 
  205. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  206. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  207. <br>
  208. <b>ios::showpos</b> 
  209. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  210. <a href="%s7p8"><img src=iicons/bullbib.gif></a>
  211.  
  212. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  213. <br>
  214. <b>iostream</b> 
  215. <a href="%s2p12"><img src=iicons/bullbib.gif></a>
  216. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  217. <br>
  218. <b>istream</b> class 
  219. <a href="%s2p8"><img src=iicons/bullbib.gif></a>
  220. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  221. <br>
  222. L<br>
  223. leading <b>0</b> 
  224. <a href="%s7p12"><img src=iicons/bullbib.gif></a>
  225. <br>
  226. </font>
  227.  
  228. </page>
  229. <page>
  230. <font size=14>
  231. leading <b>0x</b> or leading <b>0X</b> 
  232.  
  233. <a href="%s7p12"><img src=iicons/bullbib.gif></a>
  234. <br>
  235. <b>left</b> 
  236. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  237. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  238. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  239. <br>
  240. left justify 
  241. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  242. <br>
  243. O<br>
  244. <b>oct</b> 
  245. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  246. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  247. <a href="%s7p11"><img src=iicons/bullbib.gif></a>
  248. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  249. <br>
  250. <b>ofstream</b> 
  251. <a href="%s2p12"><img src=iicons/bullbib.gif></a>
  252. <br>
  253. <b>operator void*</b> member 
  254. function 
  255. <a href="%s8p4"><img src=iicons/bullbib.gif></a>
  256. <br>
  257. <b>operator!</b> member 
  258. function 
  259. <a href="%s8p4"><img src=iicons/bullbib.gif></a>
  260. <br>
  261. <b>ostream</b> class 
  262. <a href="%s2p8"><img src=iicons/bullbib.gif></a>
  263. <a href="%s2p10"><img src=iicons/bullbib.gif></a>
  264. <br>
  265. P<br>
  266. padding 
  267. <a href="%s6p6"><img src=iicons/bullbib.gif></a>
  268. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  269. <br>
  270. </font>
  271.  
  272. </page>
  273. <page>
  274. <font size=14>
  275. parameterized stream 
  276. manipulator 
  277. <a href="%s2p6"><img src=iicons/bullbib.gif></a>
  278. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  279. <a href="%s6p9"><img src=iicons/bullbib.gif></a>
  280.  
  281. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  282. <br>
  283. <b>peek</b> 
  284. <a href="%s4p12"><img src=iicons/bullbib.gif></a>
  285. <br>
  286. <b>precision</b> member 
  287. function 
  288. <a href="%s7p13"><img src=iicons/bullbib.gif></a>
  289. <br>
  290. <b>put</b> member function 
  291. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  292.  
  293. <a href="%s3p11"><img src=iicons/bullbib.gif></a>
  294. <br>
  295. <b>putback</b> 
  296. <a href="%s4p11"><img src=iicons/bullbib.gif></a>
  297. <br>
  298. R<br>
  299. <b>rdstate</b> member function 
  300.  
  301. <a href="%s8p2"><img src=iicons/bullbib.gif></a>
  302. <br>
  303. <b>read</b> 
  304. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  305. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  306. <br>
  307. </font>
  308.  
  309. </page>
  310. <page>
  311. <font size=14>
  312. <b>resetiosflags</b> stream 
  313. manipulator 
  314. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  315. <br>
  316. right justification 
  317. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  318. <a href="%s7p6"><img src=iicons/bullbib.gif></a>
  319. <br>
  320. S<br>
  321. <b>setbase</b> 
  322. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  323. <br>
  324. <b>setf</b> member function 
  325. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  326.  
  327. <a href="%s7p18"><img src=iicons/bullbib.gif></a>
  328. <br>
  329. <b>setfill</b> manipulator 
  330. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  331.  
  332. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  333. <br>
  334. <b>setiosflags</b> 
  335. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  336. <a href="%s7p6"><img src=iicons/bullbib.gif></a>
  337. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  338. <br>
  339. <b>setprecision</b> 
  340. <a href="%s6p3"><img src=iicons/bullbib.gif></a>
  341. <br>
  342. <b>setw</b> 
  343. <a href="%s6p7"><img src=iicons/bullbib.gif></a>
  344. <a href="%s7p6"><img src=iicons/bullbib.gif></a>
  345. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  346. <br>
  347. <b>skipws</b> 
  348. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  349. <br>
  350. stream input 
  351. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  352. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  353. <br>
  354. </font>
  355.  
  356. </page>
  357. <page>
  358. <font size=14>
  359. stream output 
  360. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  361. <br>
  362. stream-extraction 
  363. operator (<b>>></b>) 
  364. <a href="^Errors::c:s0p2"><img src=iicons/bullbib.gif></a>
  365. <br>
  366. stream-insertion operator 
  367. (<b><<</b>) 
  368. <a href="%s2p9"><img src=iicons/bullbib.gif></a>
  369. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  370. <a href="^Errors::c:s0p2"><img src=iicons/bullbib.gif></a>
  371. <br>
  372. T<br>
  373. <b>tie</b> 
  374. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  375. <br>
  376. type-safe I/O 
  377. <a href="%s4p13"><img src=iicons/bullbib.gif></a>
  378. <br>
  379. U<br>
  380. unformatted I/O 
  381. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  382. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  383. <br>
  384. <b>unsetf</b> member function 
  385.  
  386. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  387. <a href="%s7p19"><img src=iicons/bullbib.gif></a>
  388. <br>
  389. uppercase 
  390. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  391. <br>
  392. W<br>
  393. </font>
  394.  
  395. </page>
  396. <page>
  397. <font size=14>
  398. whitespace 
  399. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  400. <a href="%s4p5"><img src=iicons/bullbib.gif></a>
  401. <a href="%s4p6"><img src=iicons/bullbib.gif></a>
  402.  
  403. <a href="%s4p8"><img src=iicons/bullbib.gif></a>
  404. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  405. <a href="^Illustration::c:s0p4"><img src=iicons/bullbib.gif></a>
  406. <br>
  407. <b>width</b> 
  408. <a href="%s6p6"><img src=iicons/bullbib.gif></a>
  409. <a href="^Errors::c:s0p3"><img src=iicons/bullbib.gif></a>
  410. <br>
  411. <b>write</b> member function 
  412.  
  413. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  414. <br>
  415. <b>ws</b> stream manipulator 
  416.  
  417. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  418. <br>
  419. <br>
  420. </font>
  421.  
  422. </page>
  423. </section>
  424. <section type=Popup name=Portable title="Portability">
  425. <page>
  426. When prompting the 
  427. user on how to end 
  428. input from the 
  429. keyboard, ask the user 
  430. to "enter end-of-file to 
  431. end input" rather than 
  432. prompting for <ctrl>-d 
  433. (UNIX and Macintosh) 
  434. or <ctrl>-z (PC and 
  435. VAX).<br>
  436.  
  437. </page>
  438. </section>
  439. <section type=Popup name=Quotes title="Quotes">
  440. <page>
  441. <i>Consciousness ... does not 
  442. appear to itself chopped 
  443. up in bits ... A "river" or 
  444. a "stream" are the 
  445. metaphors by<p>
  446. which it is most naturally 
  447. described. </i> <br>
  448. William James<br>
  449. <br>
  450.  
  451. </page>
  452. <page>
  453. <i>All the news that's fit to 
  454. print.</i>  <br>
  455. Adolph S. Ochs<br>
  456. <br>
  457.  
  458. </page>
  459. </section>
  460. <section type=Popup name=Illustration title="Illustrations">
  461. <page>
  462. <a href="^Illustration::c:s0p2">Fig. 11.1</a>  Portion of the stream I/O class hierarchy.<br>
  463. <a href="^Illustration::c:s0p3">Fig. 11.2</a>  Portion of stream-I/O class hierarchy with key file-processing classes.<br>
  464. <a href="^Code::c:s0p0">Fig. 11.3</a>  Outputting a string using stream insertion.<br>
  465. <a href="^Code::c:s0p1">Fig. 11.4</a>  Outputting a string using two stream insertions.<br>
  466. <a href="^Code::c:s0p2">Fig. 11.5</a>  Using the <b>endl</b> stream manipulator.<br>
  467. <a href="^Code::c:s0p3">Fig. 11.6</a>  Outputting expression values.<br>
  468. <a href="^Code::c:s0p4">Fig. 11.7</a>  Cascading the overloaded <b><<</b> operator.<br>
  469. <a href="^Code::c:s0p5">Fig. 11.8</a>  Printing the address stored in a <b>char *</b> variable.<br>
  470. <a href="^Code::c:s0p6">Fig. 11.9</a>  Calculating the sum of two integers input from the keyboard with <b>cin</b> and the 
  471. stream-extraction operator.<br>
  472. <a href="^Code::c:s0p7">Fig. 11.10</a>  Avoiding a precedence problem between the stream-insertion operator and the 
  473. conditional operator.<br>
  474. <a href="^Code::c:s0p8">Fig. 11.11</a>  Stream-extraction operator returning false on end-of-file.<br>
  475. <a href="^Code::c:s0p9">Fig. 11.12</a>  Using member functions <b>get</b>, <b>put</b>, and <b>eof</b>.<br>
  476. <a href="^Code::c:s0p10">Fig. 11.13</a>  Contrasting input of a string using cin with stream extraction and input with 
  477. <b>cin.get</b>.<br>
  478. <a href="^Code::c:s0p11">Fig. 11.14</a>  Character input with member function <b>getline</b>.<br>
  479. <a href="^Code::c:s0p12">Fig. 11.15</a>  Unformatted I/O with the <b>read</b>, <b>gcount</b> and <b>write</b> member functions.<br>
  480. <a href="^Code::c:s0p13">Fig. 11.16</a>  Using the <b>hex</b>, <b>oct</b>, <b>dec</b> and <b>setbase</b> stream manipulators.<br>
  481.  
  482. </page>
  483. <page>
  484. <a href="^Code::c:s0p14">Fig. 11.17</a>  Controlling precision of floating-point values.<br>
  485. <a href="^Code::c:s0p15">Fig. 11.18</a>  Demonstrating the <b>width</b> member function.<br>
  486. <a href="^Code::c:s0p16">Fig. 11.19</a>  Creating and testing user-defined, nonparameterized stream manipulators.<br>
  487. <a href="^Illustration::c:s0p4">Fig. 11.20</a>  Format state flags.<br>
  488. <a href="^Code::c:s0p17">Fig. 11.21</a>  Controlling the printing of trailing zeros and decimal points with float values.<br>
  489. <a href="^Code::c:s0p18">Fig. 11.22</a>  Left-justification and right-justification.<br>
  490. <a href="^Code::c:s0p19">Fig. 11.23</a>  Printing an integer with internal spacing and forcing the plus sign.<br>
  491. <a href="^Code::c:s0p20">Fig. 11.24</a>  Using the <b>fill</b> member function and the <b>setfill</b> manipulator to change the padding 
  492. character for fields larger than the values being printed.<br>
  493. <a href="^Code::c:s0p21">Fig. 11.25</a>  Using the <b>ios::showbase</b> flag.<br>
  494. <a href="^Code::c:s0p22">Fig. 11.26</a>  Displaying floating-point values in system default, scientific, and fixed formats.<br>
  495. <a href="^Code::c:s0p23">Fig. 11.27</a>  Using the <b>ios::uppercase</b> flag.<br>
  496. <a href="^Code::c:s0p24">Fig. 11.28</a>  Demonstrating the <b>flags</b> member function.<br>
  497. <a href="^Code::c:s0p25">Fig. 11.29</a>  Testing error states.<br>
  498. <br>
  499.  
  500. </page>
  501. <page>
  502. <font size=18><a href="~audio/Ch11/11fig001.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 11.1 - Portion of the stream I/O class hierarchy.<img src="graphics/ch11/fig11001.gif" ></font><br>
  503.  
  504. </page>
  505. <page>
  506. <font size=18><a href="~audio/Ch11/11fig002.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 11.2 - Portion of stream-I/O class hierarchy with key file-processing classes.</font><br>
  507. <img src="graphics/ch11/fig11002.gif" ><br>
  508.  
  509. </page>
  510. <page>
  511. <font size=18><a href="~audio/Ch11/11fig020.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 11.20 - Format state flags. (Part 1 of 2) <img src="graphics/ch11/fig1120a.gif" ></font><br>
  512.  
  513. </page>
  514. <page>
  515. <font size=18>Figure 11.20 - Format state flags. (Part 2 of 2)<img src="graphics/ch11/fig1120b.gif" ></font><br>
  516.  
  517. </page>
  518. </section>
  519. <section type=Popup name=Answers title="Answers">
  520. <page pagename="Answers 11.1">
  521. <b>Answers 11.1</b><br>
  522. <font size=2><br></font><font size=11><pre>
  523. a) friend<p>
  524. b) ios::left, ios::right, and ios::internal.<p>
  525. c) streams.<p>
  526. d) setiosflags, resetiosflags.<p>
  527. e) iostream.h.<p>
  528. f) setf, unsetf.<p>
  529. g) strstream.h<p>
  530. h) iomanip.h.<p>
  531. i) fstream.h.<p>
  532. j) endl.<p>
  533. </pre></font>
  534. <foreign  name="exercises" url="^Exercises::c:s0p0">
  535.  
  536. </page>
  537. <page pagename="Answers 11.1">
  538. <font size=2><br></font><font size=11><pre>
  539. k) stdiostream.h.  <p>
  540. </pre></font>
  541. <font size=2><br></font><font size=11><pre>
  542. l) write.  <p>
  543. m) istream.<p>
  544. n) cerr or clog.<p>
  545. o) ostream.<p>
  546. p) <<. <p>
  547. q) cin, cout, cerr, and clog.<p>
  548. r) >>.<p>
  549. s) oct, hex, dec.<p>
  550. t) six digits of precision.<p>
  551. u) ios::showpos.<p>
  552. </pre></font>
  553. <foreign  name="exercises" url="^Exercises::c:s0p0">
  554.  
  555. </page>
  556. <page pagename="Answers 11.2">
  557. <b>Answers 11.2</b><br>
  558. a)  True.<br>
  559. b)  False. The stream-insertion and stream-extraction operators are not 
  560. overloaded for all user-defined types. The programmer of a class must 
  561. specifically provide the overloaded operator functions to overload the stream 
  562. operators for use with each user-defined type.<br>
  563. c)  False. The stream member function <b>flags()</b> with no arguments simply returns 
  564. the current value of the <b>flags</b> state variable.<br>
  565. d)  True.<br>
  566. e)  True.<br>
  567. <foreign  name="exercises" url="^Exercises::c:s0p4">
  568.  
  569. </page>
  570. <page pagename="Answers 11.2">
  571. f)  False. To overload the stream-insertion operator <b><<</b>, the overloaded operator 
  572. function must take an <b>ostream</b> reference and a reference to a user-defined type as 
  573. arguments, and return an <b>ostream</b> reference.<br>
  574. g)  True. Unless <b>ios::skipws</b> is off.<br>
  575. h)  False. The I/O features of C++ are provided as part of the C++ Standard 
  576. Library. The C++ language does not contain capabilities for input, output, or file 
  577. processing.<br>
  578. i)  True.<br>
  579. j)  True.<br>
  580. k)  True.<br>
  581. <foreign  name="exercises" url="^Exercises::c:s0p4">
  582. <foreign >
  583. m)  True.<br>
  584. n)  True.<br>
  585. o)  True.<br>
  586. p)  False. The <b>ostream</b> member function <b>put</b> outputs its single character 
  587. argument.<br>
  588. q)  False. The stream manipulators <b>dec</b>, <b>oct</b>, and <b>hex</b> set the output format state 
  589. for integers to the specified base until the base is changed again or the program 
  590. terminates.<br>
  591. <foreign  name="exercises" url="^Exercises::c:s0p4">
  592.  
  593. </page>
  594. <page pagename="Answers 11.2">
  595.  
  596. </page>
  597. <page pagename="Answers 11.2">
  598. r)  False. Memory addresses are displayed in hexadecimal format by default. To 
  599. display addresses as <b>long</b> integers, the address must be cast to a <b>long</b> value.<br>
  600. <foreign  name="exercises" url="^Exercises::c:s0p4">
  601. <br>
  602.  
  603. </page>
  604. <page pagename="Answers 11.3">
  605. <b>Answers 11.3</b><br>
  606. <font size=2><br></font><font size=11><pre>
  607. a)  cout << "Enter your name: ";<p>
  608. b)  cout.setf(ios::uppercase);<p>
  609. c)  cout << (void *) string;<p>
  610. d)  cout.setf(ios::scientific, ios::floatfield);<p>
  611. e)  cout << integerPtr;<p>
  612. f)  cout << setiosflags(ios::showbase);<p>
  613. g)  cout << *floatPtr;<p>
  614. h)  cout.fill( '*' );<p>
  615. cout << setfill( '*' );<p>
  616. i)  cout.put( 'O' ).put( 'K' );<p>
  617. </pre></font>
  618. <foreign  name="exercises" url="^Exercises::c:s0p7">
  619.  
  620. </page>
  621. <page pagename="Answers 11.3">
  622. <font size=2><br></font><font size=11><pre>
  623. j)  cin.peek();<p>
  624. </pre></font>
  625. <font size=2><br></font><font size=11><pre>
  626. k)  c = cin.get()<p>
  627. cin.get( c );<p>
  628. l)  cin.ignore( 6 );<p>
  629. m)  cin.read( line, 50 );<p>
  630. n)  cin.get( name, 10, '.' );<p>
  631. cin.getline( name, 10, '.' );<p>
  632. o)  cout.write( line, cin.gcount() );<p>
  633. p)  cout.flush();<p>
  634. cout << flush;<p>
  635. q)  cout << 124 << 18.376 << 'Z' << 1000000 << "String";<p>
  636. </pre></font>
  637. <foreign  name="exercises" url="^Exercises::c:s0p7">
  638.  
  639. </page>
  640. <page pagename="Answers 11.3">
  641. <font size=2><br></font><font size=11><pre>
  642. r)  cout << cout.precision();<p>
  643. </pre></font>
  644. <font size=2><br></font><font size=11><pre>
  645. s)  cin >> months >> percentageRate;<p>
  646. t)  cout << setprecision( 3 ) << 1.92 << '\ '<p>
  647.      << 1.925 << '\ ' << 1.9258;<p>
  648. u)  cout << oct << 100 << hex << 100 << dec << 100;<p>
  649. v)  cout << 100 << setbase( 8 ) << 100 << setbase( 16 ) << 100;<p>
  650. w)  cout << setw( 10 ) << 1234;<p>
  651. x)  cin.get( line, 20, 'z' );<p>
  652. y)  cout << setw( x ) << setprecision( y ) << 87.4573;<p>
  653. </pre></font>
  654. <foreign  name="exercises" url="^Exercises::c:s0p7">
  655. <br>
  656.  
  657. </page>
  658. <page pagename="Answer 11.4">
  659. <b>Answer 11.4</b><br>
  660. a)  Error: The precedence of the <b><<</b> operator is higher than the precedence of <b><=</b> 
  661. which causes the statement to be evaluated improperly and causes a compiler  
  662. error. <br>
  663.      Correction: To correct the statement, add parentheses around the expression <b>x</b> 
  664. <b><= y</b>. This problem will occur with any expression that uses operators of lower 
  665. precedence than the <b><<</b> operator if the expression is not placed in parentheses.<br>
  666. b)  Error: In C++, characters are not treated as small integers as they are in C.<br>
  667.      Correction: To print the numerical value for a character in the computer's 
  668. character set, the character must be cast to an integer value as in the following:<br>
  669. <font size=2><br></font><font size=11><pre>
  670. cout << int('c');<p>
  671. </pre></font>
  672. <foreign  name="exercises" url="^Exercises::c:s0p12">
  673.  
  674. </page>
  675. <page pagename="Answer 11.4">
  676. c)  Error: Quote characters cannot be printed in a string unless an escape 
  677. sequence is used.<br>
  678.      Correction: Print the string in one of the following ways:<br>
  679. <font size=2><br></font><font size=11><pre>
  680. cout << '"' << "A string in quotes" << '"';<p>
  681. cout << "\\"A string in quotes\\"";<p>
  682. </pre></font>
  683. <foreign  name="exercises" url="^Exercises::c:s0p12">
  684. <br>
  685.  
  686. </page>
  687. <page pagename="Anwers 11.5">
  688. <b>Anwers 11.5</b><br>
  689. <font size=2><br></font><font size=11><pre>
  690. a)  12345<p>
  691.     **123<p>
  692.     123<p>
  693. b)  $$$$$10000<p>
  694. c)  1024.988<p>
  695. d)  0143<p>
  696.     0x63<p>
  697. e)  100000<p>
  698.     +100000<p>
  699. f)    4.45e+02<p>
  700. </pre></font>
  701. <foreign  name="exercises" url="^Exercises::c:s0p13">
  702.  
  703. </page>
  704. <page pagename="Answer 11.7">
  705. <b>Answer 11.7</b><br>
  706. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  707. the file cpphtp2/answers/P11_07.zip to your hard drive and unzip the program 
  708. code.<br>
  709. <foreign  name="exercises" url="^Exercises::c:s0p17">
  710. <br>
  711.  
  712. </page>
  713. <page pagename="Answer 11.9">
  714. <b>Answer 11.9</b><br>
  715. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  716. the file cpphtp2/answers/P11_09.zip to your hard drive and unzip the program 
  717. code.<br>
  718. <foreign  name="exercises" url="^Exercises::c:s0p19">
  719. <br>
  720.  
  721. </page>
  722. <page pagename="Answer 11.10">
  723. <b>Answer 11.10</b><br>
  724. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  725. the file cpphtp2/answers/P11_10.zip to your hard drive and unzip the program 
  726. code.<br>
  727. <foreign  name="exercises" url="^Exercises::c:s0p20">
  728. <br>
  729.  
  730. </page>
  731. <page pagename="Answer 11.12">
  732. <b>Answer 11.12</b><br>
  733. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  734. the file cpphtp2/answers/P11_12.zip to your hard drive and unzip the program 
  735. code.<br>
  736. <foreign  name="exercises" url="^Exercises::c:s0p22">
  737. <br>
  738.  
  739. </page>
  740. <page pagename="Answer 11.15">
  741. <b>Answer 11.15</b><br>
  742. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  743. the file cpphtp2/answers/P11_15.zip to your hard drive and unzip the program 
  744. code.<br>
  745. <foreign  name="exercises" url="^Exercises::c:s0p26">
  746. <br>
  747.  
  748. </page>
  749. <page pagename="Answer 11.18">
  750. <b>Answer 11.18</b><br>
  751. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  752. the file cpphtp2/answers/P11_18.zip to your hard drive and unzip the program 
  753. code.<br>
  754. <foreign  name="exercises" url="^Exercises::c:s0p30">
  755. <br>
  756.  
  757. </page>
  758. </section>
  759. <section type=Popup name=Exercises title="Exercises">
  760. <page pagename="Exercise 11.1">
  761. <b>Exercise 11.1</b><br>
  762. Answer each of the following:<br>
  763. a)  Overloaded stream operators are often defined as ________ functions of a 
  764. class. <br>
  765. b)  The format justification bits that can be set include ________, 
  766. _________, and<p>
  767. ________.<br>
  768. c)  Input/output in C++ occurs as _________ of bytes.<br>
  769. d)  Parameterized stream manipulators ________ and ________ can be used 
  770. to set and reset format state flags.<br>
  771. e)  Most C++ programs should include the _________ header file that contains 
  772. basic information required for all stream I/O operations.<br>
  773. <foreign  name="answers" url="^Answers::c:s0p0">
  774.  
  775. </page>
  776. <page pagename="Exercise 11.1">
  777. f)  Member functions _______ and ______ are used to set and reset format 
  778. state flags.<br>
  779. g)  Header file _________ contains information for performing "in-memory" 
  780. formatting.<br>
  781. h)  When using parameterized manipulators, the header file ________ must be 
  782. included.<br>
  783. i)  Header file ________ contains information for user controlled file 
  784. processing.<br>
  785. j)  The ________ stream manipulator inserts a newline character in the output 
  786. stream and flushes the output stream.<br>
  787. k)  Header file ________ is used in programs that mix C-style and C++-style I/
  788. O.<br>
  789. <foreign  name="answers" url="^Answers::c:s0p0">
  790.  
  791. </page>
  792. <page pagename="Exercise 11.1">
  793. l)  The ostream member function ________ is used to perform unformatted 
  794. output.<br>
  795. m)  Input operations are supported by the  ________ class.<br>
  796. n)  Outputs to the standard error stream are directed to either the  ________ or 
  797. the<p>
  798.  ________ stream object.<br>
  799. o)  Output operations are supported by the  ________ class.<br>
  800. p)  The symbol for the stream-insertion operator is ________.<br>
  801. q)  The four objects that correspond to the standard devices on the system 
  802. include<p>
  803.  ________, ________, ________, and ________.          <br>
  804. r)  The symbol for the stream-extraction operator is ________.<br>
  805. <foreign  name="answers" url="^Answers::c:s0p0">
  806.  
  807. </page>
  808. <page pagename="Exercise 11.1">
  809. s)  The stream manipulators ________,  ________, and ________  are 
  810. used to specify that integers should be displayed in octal, hexadecimal, and 
  811. decimal formats.<br>
  812. t)  The default precision for displaying floating-point values is ________.<br>
  813. u)  When set, the ________ flag causes positive numbers to display with a plus 
  814. sign.<br>
  815. <foreign  name="answers" url="^Answers::c:s0p0">
  816. <br>
  817.  
  818. </page>
  819. <page pagename="Exercise 11.2">
  820. <b>Exercise 11.2</b><br>
  821. State whether the following are true or false. If the answer is false, explain why.<br>
  822. a)  The stream member function <b>flags()</b> with a long argument sets the <b>flags</b> state 
  823. variable to its argument and returns its previous value.<br>
  824. b)  The stream-insertion operator<b> <<</b> and the stream-extraction operator<b> >></b> are 
  825. overloaded to handle all standard data types--including strings and memory 
  826. addresses (stream-insertion only)--and all user-defined data types.<br>
  827. c)  The stream member function <b>flags()</b> with no arguments resets all the flag bits 
  828. in the <b>flags</b> state variable.<br>
  829. d)  The stream-extraction operator <b>>></b> can be overloaded with an operator 
  830. function that takes an <b>istream</b> reference and a reference to a user-defined type as 
  831. arguments and returns an <b>istream</b> reference.<br>
  832. <foreign  name="answers" url="^Answers::c:s0p2">
  833.  
  834. </page>
  835. <page pagename="Exercise 11.2">
  836. e)  The ws stream manipulator skips leading whitespace in an input stream.<br>
  837. f)  The stream-insertion operator <b><<</b> can be overloaded with an operator function 
  838. that takes an <b>istream</b> reference and a reference to a user-defined type as 
  839. arguments and returns an <b>istream</b> reference.<br>
  840. g)  Input with the stream-extraction operator <b>>></b> always skips leading whitespace 
  841. characters in the input stream.<br>
  842. h)  The input and output  features are provided as part of C++.<br>
  843. i)  The stream member function <b>rdstate()</b> returns the state of the current stream.<br>
  844. j)  The <b>cout</b> stream is normally connected to the display screen.<br>
  845. k)  The stream member function good() returns true if the <b>bad(),</b> <b>fail()</b>, and<b> 
  846. eof()</b> member functions all return false.<br>
  847. l)  The <b>cin</b> stream is normally connected to the display screen.<br>
  848. <foreign  name="answers" url="^Answers::c:s0p2">
  849.  
  850. </page>
  851. <page pagename="Exercise 11.2">
  852. m)  If a nonrecoverable error occurs during a stream operation, the <b>bad</b> member 
  853. function will return true.<br>
  854. n)  Output to <b>cerr</b> is unbuffered and output to <b>clog</b> is buffered.<br>
  855. o)  When the <b>ios::showpoint</b> flag is set, floating-point values are forced to print 
  856. with the default six digits of precision--provided that the precision value has not 
  857. been changed, in which case floating-point values print with the specified 
  858. precision.<br>
  859. p)  The <b>ostream</b> member function <b>put</b> outputs the specified number of 
  860. characters.<br>
  861. q)  The stream manipulators <b>dec</b>, <b>oct</b>, and <b>hex</b> only affect the next integer output  
  862. operation.<br>
  863. r)  When output, memory addresses are displayed as <b>long</b> integers by default.<br>
  864. <foreign  name="answers" url="^Answers::c:s0p2">
  865.  
  866. </page>
  867. <page pagename="Exercise 11.3">
  868. <b>Exercise 11.3</b><br>
  869. For each of the following, write a single statement that performs the indicated 
  870. task. <br>
  871. a)  Output the string "<b>Enter your name: </b>".<br>
  872. b)  Set a flag to cause the exponent in scientific notation and the letters in 
  873. hexadecimal values to print in capital letters. <br>
  874. c)  Output the address of the variable <b>string</b> of type <b>char *</b>.<br>
  875. d)  Set a flag so that floating-point values print in scientific notation.<br>
  876. e)  Output the address of the variable <b>integerPtr</b> of type <b>int *</b>.<br>
  877. f)  Set a flag so that when integer values are output the integer base for octal and 
  878. hexadecimal values is displayed.<br>
  879. g)  Output the value pointed to by <b>floatPtr</b> of type <b>float *</b>.<br>
  880. <foreign  name="answers" url="^Answers::c:s0p6">
  881.  
  882. </page>
  883. <page pagename="Exercise 11.3">
  884. h)  Use a stream member function to set the fill character to '<b>*</b>' for printing in 
  885. field widths larger than the values being output. Write a separate statement to do 
  886. this with a stream manipulator.<br>
  887. i)  Output the characters '<b>O</b>' and '<b>K</b>' in one statement with <b>ostream</b> function 
  888. <b>put</b>.<br>
  889. j)  Get the next character in the input stream without extracting it from the 
  890. stream.<br>
  891. k)  Input a single character into variable <b>c</b> of type <b>char</b> using the <b>istream</b> 
  892. member function <b>get</b> in two different ways.<br>
  893. l)  Input and discard the next six characters in the input stream.<br>
  894. m)  Use the <b>istream</b> member function <b>read</b> to input 50 characters into array <b>line</b> 
  895. of type <b>char</b>.<br>
  896. <foreign  name="answers" url="^Answers::c:s0p6">
  897.  
  898. </page>
  899. <page pagename="Exercise 11.3">
  900. n)  Read 10 characters into character array <b>name</b>. Stop reading characters if the 
  901. '<b>.</b>' delimiter is encountered. Do not remove the delimiter from the input stream. 
  902. Write another statement that performs this task and removes the delimiter from 
  903. the input.<br>
  904. o)  Use the <b>istream</b> member function <b>gcount</b> to determine the number of 
  905. characters input into character array <b>line</b> by the last call to <b>istream</b> member 
  906. function <b>read</b> and output that number of characters using <b>ostream</b> member 
  907. function <b>write</b>.<br>
  908. p)  Write separate statements to flush the output stream using a member function 
  909. and a stream manipulator.<br>
  910. q)  Output the following values: <b>124</b>, <b>18.376</b>, '<b>Z</b>', <b>1000000</b>, and "<b>String</b>".<br>
  911. r)  Print the current precision setting using a member function.<br>
  912. <foreign  name="answers" url="^Answers::c:s0p6">
  913.  
  914. </page>
  915. <page pagename="Exercise 11.3">
  916. s)  Input an integer value into <b>int</b> variable <b>months</b> and a floating-point value into 
  917. <b>float</b> variable <b>percentageRate</b>.<br>
  918. t)  Print <b>1.92</b>, <b>1.925</b>, and <b>1.9258</b> with 3 digits of precision using a manipulator. <br>
  919. u)  Print integer <b>100</b> in octal, hexadecimal, and decimal using stream 
  920. manipulators.<br>
  921. v)  Print integer <b>100</b> in decimal, octal, and hexadecimal using a single stream 
  922. manipulator to change the base.<br>
  923. w)  Print <b>1234</b> right-justified in a <b>10</b> digit field. <br>
  924. x)  Read characters into character array <b>line</b> until the character '<b>z</b>' is 
  925. encountered up to a limit of <b>20</b> characters (including a terminating null 
  926. character). Do not extract the delimiter character from the stream.<br>
  927. <foreign  name="answers" url="^Answers::c:s0p6">
  928.  
  929. </page>
  930. <page pagename="Exercise 11.3">
  931. y)  Use integer variables <b>x</b> and <b>y</b> to specify the field width and precision used to 
  932. display the <b>double</b> value <b>87.4573</b> and display the value.<br>
  933. <foreign  name="answers" url="^Answers::c:s0p6">
  934. <br>
  935.  
  936. </page>
  937. <page pagename="Exercise 11.4">
  938. <b>Exercise 11.4</b><br>
  939. Identify the error in each of the following statements and explain how to correct 
  940. it.<br>
  941. <font size=2><br></font><font size=11><pre>
  942. a)  cout << "Value of x <= y is: " << x <= y;<p>
  943. </pre></font>
  944. b)  The following statement should print the integer value of ' <b>c</b>'.<br>
  945. <font size=2><br></font><font size=11><pre>
  946. cout << 'c';<p>
  947. c)  cout << ""A string in quotes"";<p>
  948. </pre></font>
  949. <foreign  name="answers" url="^Answers::c:s0p9">
  950. <br>
  951.  
  952. </page>
  953. <page pagename="Exercise 11.5">
  954. <b>Exercise 11.5</b><br>
  955. For each of the following, show the output.<br>
  956. <font size=2><br></font><font size=11><pre>
  957. a)  cout << "12345" << endl;<p>
  958.     cout.width( 5 );<p>
  959.     cout.fill( '*' );<p>
  960.     cout << 123 << endl << 123;<p>
  961. b)  cout << setw( 10 ) << setfill( '$' ) << 10000;<p>
  962. c)  cout << setw( 8 ) << setprecision( 3 ) << 1024.987654;<p>
  963. d)  cout << setiosflags( ios::showbase ) << oct << 99<p>
  964.          << endl << hex << 99;<p>
  965. </pre></font>
  966. <foreign  name="answers" url="^Answers::c:s0p11">
  967. <font size=2><br></font><font size=11><pre>
  968. <p>
  969. </pre></font>
  970.  
  971. </page>
  972. <page pagename="Exercise 11.5">
  973. <font size=2><br></font><font size=11><pre>
  974. e)  cout << 100000 << endl<p>
  975. </pre></font>
  976. <font size=2><br></font><font size=11><pre>
  977.          << setiosflags( ios::showpos ) << 100000;<p>
  978. f)  cout << setw( 10 ) << setprecision( 2 ) <<<p>
  979.          << setiosflags( ios::scientific ) << 444.93738;<p>
  980. </pre></font>
  981. <foreign  name="answers" url="^Answers::c:s0p11">
  982. <br>
  983.  
  984. </page>
  985. <page pagename="Exercise 11.6">
  986. <b>Exercise 11.6</b><br>
  987. Write a statement for each of the following:<br>
  988. a)  Print integer <b>40000</b> left-justified in a <b>15</b>-digit field.<br>
  989. b)  Read a string into character array variable <b>state</b>.<br>
  990. c)  Print <b>200</b> with and without a sign.<br>
  991. d)  Print the decimal value <b>100</b> in hexadecimal form preceded by <b>0x</b>.<br>
  992. e)  Read characters into array <b>s</b> until the character '<b>p</b>' is encountered up to a 
  993. limit of 10 characters (including the terminating null character). Extract the 
  994. delimiter from the input stream and discard it.<br>
  995. f)  Print <b>1.234</b> in a <b>9</b>-digit field with preceding zeros.<br>
  996.  
  997. </page>
  998. <page pagename="Exercise 11.6">
  999. g)  Read a string of the form "<b>characters</b>" from the standard input. Store the 
  1000. string in character array <b>s</b>. Eliminate the quotation marks from the input stream. 
  1001. Read a maximum of 50 characters (including the terminating null character).<br>
  1002. <br>
  1003.  
  1004. </page>
  1005. <page pagename="Exercise 11.7">
  1006. <b>Exercise 11.7</b><br>
  1007. Write a program to test inputting integer values in decimal, octal, and 
  1008. hexadecimal format. Output each integer read by the program in all three 
  1009. formats. Test the program with the following input data: 10, 010, 0x10.<br>
  1010. <foreign  name="answers" url="^Answers::c:s0p12">
  1011. <br>
  1012.  
  1013. </page>
  1014. <page pagename="Exercise 11.8">
  1015. <b>Exercise 11.8</b><br>
  1016. Write a program that prints pointer values using casts to all the integer data 
  1017. types. Which ones print strange values? Which ones cause errors? <br>
  1018. <br>
  1019.  
  1020. </page>
  1021. <page pagename="Exercise 11.9">
  1022. <b>Exercise 11.9</b><br>
  1023. Write a program to test the results of printing the integer value <b>12345</b> and the 
  1024. floating-point value <b>1.2345</b> in various size fields. What happens when the values 
  1025. are printed in fields containing fewer digits than the values?<br>
  1026. <foreign  name="answers" url="^Answers::c:s0p13">
  1027. <br>
  1028.  
  1029. </page>
  1030. <page pagename="Exercise 11.10">
  1031. <b>Exercise 11.10</b><br>
  1032. Write a program that prints the value <b>100.453627</b> rounded to the nearest digit, 
  1033. tenth, hundredth, thousandth, and ten thousandth.  <br>
  1034. <foreign  name="answers" url="^Answers::c:s0p14">
  1035. <br>
  1036.  
  1037. </page>
  1038. <page pagename="Exercise 11.11">
  1039. <b>Exercise 11.11</b><br>
  1040. Write a program that inputs a string from the keyboard and determines the length 
  1041. of the string. Print the string using twice the length as the field width.<br>
  1042. <br>
  1043.  
  1044. </page>
  1045. <page pagename="Exercise11.12">
  1046. <b>Exercise11.12</b><br>
  1047. Write a program that converts integer Fahrenheit temperatures from <b>0</b> to <b>212</b> 
  1048. degrees to floating-point Celsius temperatures with 3 digits of precision. Use the 
  1049. formula <br>
  1050. <font size=2><br></font><font size=11><pre>
  1051. celsius = 5.0 / 9.0 * ( fahrenheit - 32 );<p>
  1052. </pre></font>
  1053. to  perform the calculation. The output should be printed in two right-justified 
  1054. columns, and the Celsius temperatures should be preceded by a sign for both 
  1055. positive and negative values.<br>
  1056. <foreign  name="answers" url="^Answers::c:s0p15">
  1057. <br>
  1058.  
  1059. </page>
  1060. <page pagename="Exercise 11.13">
  1061. <b>Exercise 11.13</b><br>
  1062. In some programming languages, strings are entered surrounded by either single 
  1063. or double quotation marks. Write a program that reads the three strings <b>suzy</b>, 
  1064. "<b>suzy</b>", and '<b>suzy</b>'. Are the single and double quotes ignored or read as part of 
  1065. the string?<br>
  1066. <br>
  1067.  
  1068. </page>
  1069. <page pagename="Exercise 11.14">
  1070. <b>Exercise 11.14</b><br>
  1071. In Fig. 8.3, the stream-extraction and -insertion operators were overloaded for 
  1072. input and output of objects of the <b>PhoneNumber</b> class. Rewrite the stream-
  1073. extraction operator to perform the following error checking on input. The 
  1074. <b>operator>></b> function will need to be entirely recoded. <br>
  1075. a)  Input the entire phone number into an array. Test that the proper number of 
  1076. characters have been entered. There should be a total of 14 characters read for a 
  1077. phone number of the form <b>(800) 555-1212</b>. Use the stream member function 
  1078. <b>clear</b> to set <b>ios::failbit</b> for improper input. <br>
  1079. b)  The area code and exchange do not begin with <b>0</b> or <b>1</b>. Test the first digit of 
  1080. the area code and exchange portions of the phone number to be sure that neither <br>
  1081.  
  1082. </page>
  1083. <page pagename="Exercise 11.14">
  1084. begins with 0 or 1. Use stream member function <b>clear</b> to set <b>ios::failbit</b> for 
  1085. improper input. <br>
  1086. c)  The middle digit of an area code used to always be <b>0</b> or <b>1</b> (although this has 
  1087. changed recently). Test the middle digit for a value of <b>0</b> or <b>1</b>. Use the stream 
  1088. member function <b>clear</b> to set <b>ios::failbit</b> for improper input. If none of the above 
  1089. operations results <b>inios::failbit</b> being set for improper input, copy the three parts 
  1090. of the telephone number into the <b>areaCode</b>, <b>exchange</b>, and <b>line</b> members of the 
  1091. <b>PhoneNumber</b> object. In the main program, if <b>ios::failbit</b> has been set on the 
  1092. input, have the program print an error message and end rather than print the 
  1093. phone number.<br>
  1094. <br>
  1095.  
  1096. </page>
  1097. <page pagename="Exercise 11.15">
  1098. <b>Exercise 11.15</b><br>
  1099. Write a program that accomplishes each of the following:<br>
  1100. a)  Create the user-defined class <b>Point</b> that contains the private integer data 
  1101. members <b>xCoordinate</b> and <b>yCoordinate</b>, and declares stream-insertion and 
  1102. stream-extraction overloaded operator functions as <b>friend</b>s of the class.<br>
  1103. b)  Define the stream-insertion and stream-extraction operator functions. The 
  1104. stream-extraction operator function should determine if the data entered is valid 
  1105. data, and if not, it should set the <b>ios::failbit</b> to indicate improper input. The 
  1106. stream-insertion operator should not be able to display the point after an input 
  1107. error occurred.<br>
  1108. c)  Write a <b>main</b> function that tests input and output of user-defined class <b>Point</b> 
  1109. using the overloaded stream-extraction and stream-insertion operators.<br>
  1110. <foreign  name="answers" url="^Answers::c:s0p16">
  1111.  
  1112. </page>
  1113. <page pagename="Exercise 11.16">
  1114. <b>Exercise 11.16</b><br>
  1115. Write a program that accomplishes each of the following:<br>
  1116. a)  Create the user-defined class <b>Complex</b> that contains the private integer data 
  1117. members <b>real</b> and <b>imaginary</b>, and declares stream-insertion and stream-
  1118. extraction overloaded operator functions as <b>friend</b>s of the class.<br>
  1119. b)  Define the stream-insertion and -extraction operator functions. The stream-
  1120. extraction operator function should determine if the data entered is valid, and if 
  1121. not, it should set <b>ios::failbit</b> to indicate improper input. The input should be of 
  1122. the form:<br>
  1123. <font size=2><br></font><font size=11><pre>
  1124. 3 + 8i<p>
  1125. </pre></font>
  1126. c)  <spacer width=20 height=1>The values can be negative or positive, and it is possible that one of the two 
  1127. values is not provided. If a value is not provided, the appropriate data member <br>
  1128.  
  1129. </page>
  1130. <page pagename="Exercise 11.16">
  1131. should be set to <b>0</b>. The stream-insertion operator should not be able to display 
  1132. the point if an input error occurred. The output format should be identical to the 
  1133. input format shown above. For negative imaginary values, a minus sign should 
  1134. be printed rather than a plus sign.<br>
  1135. d)  Write a main function that tests input and output of user-defined class 
  1136. <b>Complex</b> using the overloaded stream-extraction and stream-insertion 
  1137. operators.<br>
  1138. <br>
  1139.  
  1140. </page>
  1141. <page pagename="Exercise 11.17">
  1142. <b>Exercise 11.17</b><br>
  1143. Write a program that uses a for structure to print a table of ASCII values for the 
  1144. characters in the ASCII character set from <b>33</b> to <b>126</b>. The program should print 
  1145. the decimal value, octal value, hexadecimal value, and character value for each 
  1146. character. Use the stream manipulators <b>dec</b>, <b>oct</b>, and <b>hex</b> to print the integer 
  1147. values.<br>
  1148. <br>
  1149.  
  1150. </page>
  1151. <page pagename="Exercise 11.18">
  1152. <b>Exercise 11.18</b><br>
  1153. Write a program to show that the <b>getline</b> and three-argument <b>get</b> istream 
  1154. member functions each end the input string with a string terminating null 
  1155. character. Also show that <b>get</b> leaves the delimiter character on the input stream 
  1156. while <b>getline</b> extracts the delimiter character and discards it. What happens to 
  1157. the unread characters in the stream?<br>
  1158. <foreign  name="answers" url="^Answers::c:s0p17">
  1159. <br>
  1160.  
  1161. </page>
  1162. <page pagename="Exercise 11.19">
  1163. <b>Exercise 11.19</b><br>
  1164. Write a program that creates the user-defined manipulator <b>skipwhite</b> to skip 
  1165. leading whitespace characters in the input stream. The manipulator should use 
  1166. the <b>isspace</b> function from the <b>ctype.h</b> library to test if the character is a 
  1167. whitespace character. Each character should be input using the <b>istream</b> member 
  1168. function <b>get</b>. When a non-whitespace character is encountered, the <b>skipwhite</b> 
  1169. manipulator finishes its job by placing the character back on the input stream 
  1170. and returning an <b>istream</b> reference.<br>
  1171. <spacer width=20 height=1>Test the manipulator by creating a <b>main</b> function in which the <b>ios::skipws</b> flag 
  1172. is unset so that the stream-extraction operator does not automatically skip 
  1173. whitespace. Then test the manipulator on the input stream by entering a <br>
  1174.  
  1175. </page>
  1176. <page pagename="Exercise 11.19">
  1177. character preceded by whitespace as input. Print the character that was input to 
  1178. confirm that a whitespace character was not input.<br>
  1179. <br>
  1180.  
  1181. </page>
  1182. </section>
  1183. <section type=Popup name=Code title="Code Examples">
  1184. <page>
  1185. <font size=18>Figure 11.3  Outputting a string using stream insertion.</font><br>
  1186. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1187. <param  name="file" value="code/ch11/fig11_03.txt">
  1188. </applet><br>
  1189. <br>
  1190. <foreign  name="copy2disk" url="!jcpy Source/fig11_03.jar">
  1191. <foreign  name="audio" url="~audio/Ch11/11fig003.au">
  1192. <foreign  name="execute" url="!jarexe Run/fig11_03.jar">
  1193. <br>
  1194.  
  1195. </page>
  1196. <page>
  1197. <font size=18>Figure 11.4  Outputting a string using two stream insertions.</font><br>
  1198. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1199. <param  name="file" value="code/ch11/fig11_04.txt">
  1200. </applet><br>
  1201. <br>
  1202. <foreign  name="copy2disk" url="!jcpy Source/fig11_04.jar">
  1203. <foreign  name="audio" url="~audio/Ch11/11fig004.au">
  1204. <foreign  name="execute" url="!jarexe Run/fig11_04.jar">
  1205. <br>
  1206.  
  1207. </page>
  1208. <page>
  1209. <font size=18>Figure 11.5  Using the <b>endl</b> stream manipulator.</font><br>
  1210. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1211. <param  name="file" value="code/ch11/fig11_05.txt">
  1212. </applet><br>
  1213. <br>
  1214. <foreign  name="copy2disk" url="!jcpy Source/fig11_05.jar">
  1215. <foreign  name="audio" url="~audio/Ch11/11fig005.au">
  1216. <foreign  name="execute" url="!jarexe Run/fig11_05.jar">
  1217. <br>
  1218.  
  1219. </page>
  1220. <page>
  1221. <font size=18>Figure 11.6  Outputting expression values.</font><br>
  1222. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1223. <param  name="file" value="code/ch11/fig11_06.txt">
  1224. </applet><br>
  1225. <br>
  1226. <foreign  name="copy2disk" url="!jcpy Source/fig11_06.jar">
  1227. <foreign  name="audio" url="~audio/Ch11/11fig006.au">
  1228. <foreign  name="execute" url="!jarexe Run/fig11_06.jar">
  1229. <br>
  1230.  
  1231. </page>
  1232. <page>
  1233. <font size=18>Figure 11.7  Cascading the overloaded <b><<</b> operator.</font><br>
  1234. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1235. <param  name="file" value="code/ch11/fig11_07.txt">
  1236. </applet><br>
  1237. <br>
  1238. <foreign  name="copy2disk" url="!jcpy Source/fig11_07.jar">
  1239. <foreign  name="audio" url="~audio/Ch11/11fig007.au">
  1240. <foreign  name="execute" url="!jarexe Run/fig11_07.jar">
  1241. <br>
  1242.  
  1243. </page>
  1244. <page>
  1245. <font size=18>Figure 11.8  Printing the address stored in a <b>char *</b> variable.</font><br>
  1246. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1247. <param  name="file" value="code/ch11/fig11_08.txt">
  1248. </applet><br>
  1249. <br>
  1250. <foreign  name="copy2disk" url="!jcpy Source/fig11_08.jar">
  1251. <foreign  name="audio" url="~audio/Ch11/11fig008.au">
  1252. <foreign  name="execute" url="!jarexe Run/fig11_08.jar">
  1253. <br>
  1254.  
  1255. </page>
  1256. <page>
  1257. <font size=18>Figure 11.9  Calculating the sum of two integers input from the keyboard with <b>cin</b> 
  1258. and the stream-extraction operator.</font><br>
  1259. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1260. <param  name="file" value="code/ch11/fig11_09.txt">
  1261. </applet><br>
  1262. <br>
  1263. <foreign  name="copy2disk" url="!jcpy Source/fig11_09.jar">
  1264. <foreign  name="audio" url="~audio/Ch11/11fig009.au">
  1265. <foreign  name="execute" url="!jarexe Run/fig11_09.jar">
  1266. <br>
  1267.  
  1268. </page>
  1269. <page>
  1270. <font size=18>Figure 11.10  Avoiding a precedence problem between the stream-insertion operator and the conditional operator.</font><br>
  1271. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1272. <param  name="file" value="code/ch11/fig11_10.txt">
  1273. </applet><br>
  1274. <br>
  1275. <foreign  name="copy2disk" url="!jcpy Source/fig11_10.jar">
  1276. <foreign  name="audio" url="~audio/Ch11/11fig010.au">
  1277. <foreign  name="execute" url="!jarexe Run/fig11_10.jar">
  1278. <br>
  1279.  
  1280. </page>
  1281. <page>
  1282. <font size=18>Figure 11.11  Stream-extraction operator returning false on end-of-file.</font><br>
  1283. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1284. <param  name="file" value="code/ch11/fig11_11.txt">
  1285. </applet><br>
  1286. <br>
  1287. <foreign  name="copy2disk" url="!jcpy Source/fig11_11.jar">
  1288. <foreign  name="audio" url="~audio/Ch11/11fig011.au">
  1289. <foreign  name="execute" url="!jarexe Run/fig11_11.jar">
  1290. <br>
  1291.  
  1292. </page>
  1293. <page>
  1294. <font size=18>Figure 11.12  Using member functions <b>get</b>, <b>put</b>, and <b>eof</b>.</font><br>
  1295. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1296. <param  name="file" value="code/ch11/fig11_12.txt">
  1297. </applet><br>
  1298. <br>
  1299. <foreign  name="copy2disk" url="!jcpy Source/fig11_12.jar">
  1300. <foreign  name="audio" url="~audio/Ch11/11fig012.au">
  1301. <foreign  name="execute" url="!jarexe Run/fig11_12.jar">
  1302. <br>
  1303.  
  1304. </page>
  1305. <page>
  1306. <font size=18>Figure 11.13  Contrasting input of a string using cin with stream extraction and 
  1307. input with <b>cin.get</b>.</font><br>
  1308. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1309. <param  name="file" value="code/ch11/fig11_13.txt">
  1310. </applet><br>
  1311. <br>
  1312. <foreign  name="copy2disk" url="!jcpy Source/fig11_13.jar">
  1313. <foreign  name="audio" url="~audio/Ch11/11fig013.au">
  1314. <foreign  name="execute" url="!jarexe Run/fig11_13.jar">
  1315. <br>
  1316.  
  1317. </page>
  1318. <page>
  1319. <font size=18>Figure 11.14  Character input with member function <b>getline</b>.</font><br>
  1320. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1321. <param  name="file" value="code/ch11/fig11_14.txt">
  1322. </applet><br>
  1323. <br>
  1324. <foreign  name="copy2disk" url="!jcpy Source/fig11_14.jar">
  1325. <foreign  name="audio" url="~audio/Ch11/11fig014.au">
  1326. <foreign  name="execute" url="!jarexe Run/fig11_14.jar">
  1327. <br>
  1328.  
  1329. </page>
  1330. <page>
  1331. <font size=18>Figure 11.15  Unformatted I/O with the <b>read</b>, <b>gcount</b> and <b>write</b> member functions.</font><br>
  1332. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1333. <param  name="file" value="code/ch11/fig11_15.txt">
  1334. </applet><br>
  1335. <br>
  1336. <foreign  name="copy2disk" url="!jcpy Source/fig11_15.jar">
  1337. <foreign  name="audio" url="~audio/Ch11/11fig015.au">
  1338. <foreign  name="execute" url="!jarexe Run/fig11_15.jar">
  1339. <br>
  1340.  
  1341. </page>
  1342. <page>
  1343. <font size=18>Figure 11.16  Using the <b>hex</b>, <b>oct</b>, <b>dec</b> and <b>setbase</b> stream manipulators.</font><br>
  1344. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1345. <param  name="file" value="code/ch11/fig11_16.txt">
  1346. </applet><br>
  1347. <br>
  1348. <foreign  name="copy2disk" url="!jcpy Source/fig11_16.jar">
  1349. <foreign  name="audio" url="~audio/Ch11/11fig016.au">
  1350. <foreign  name="execute" url="!jarexe Run/fig11_16.jar">
  1351. <br>
  1352.  
  1353. </page>
  1354. <page>
  1355. <font size=18>Figure 11.17  Controlling precision of floating-point values.</font><br>
  1356. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1357. <param  name="file" value="code/ch11/fig11_17.txt">
  1358. </applet><br>
  1359. <br>
  1360. <foreign  name="copy2disk" url="!jcpy Source/fig11_17.jar">
  1361. <foreign  name="audio" url="~audio/Ch11/11fig017.au">
  1362. <foreign  name="execute" url="!jarexe Run/fig11_17.jar">
  1363. <br>
  1364.  
  1365. </page>
  1366. <page>
  1367. <font size=18>Figure 11.18  Demonstrating the <b>width</b> member function.</font><br>
  1368. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1369. <param  name="file" value="code/ch11/fig11_18.txt">
  1370. </applet><br>
  1371. <br>
  1372. <foreign  name="copy2disk" url="!jcpy Source/fig11_18.jar">
  1373. <foreign  name="audio" url="~audio/Ch11/11fig018.au">
  1374. <foreign  name="execute" url="!jarexe Run/fig11_18.jar">
  1375. <br>
  1376.  
  1377. </page>
  1378. <page>
  1379. <font size=18>Figure 11.19   Creating and testing user-defined, nonparameterized stream manipulators.</font><br>
  1380. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1381. <param  name="file" value="code/ch11/fig11_19.txt">
  1382. </applet><br>
  1383. <br>
  1384. <foreign  name="copy2disk" url="!jcpy Source/fig11_19.jar">
  1385. <foreign  name="audio" url="~audio/Ch11/11fig019.au">
  1386. <foreign  name="execute" url="!jarexe Run/fig11_19.jar">
  1387. <br>
  1388.  
  1389. </page>
  1390. <page>
  1391. <font size=18>Figure 11.21  Controlling the printing of trailing zeros and decimal points with float 
  1392. values.</font><br>
  1393. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1394. <param  name="file" value="code/ch11/fig11_21.txt">
  1395. </applet><br>
  1396. <br>
  1397. <foreign  name="copy2disk" url="!jcpy Source/fig11_21.jar">
  1398. <foreign  name="audio" url="~audio/Ch11/11fig021.au">
  1399. <foreign  name="execute" url="!jarexe Run/fig11_21.jar">
  1400. <br>
  1401.  
  1402. </page>
  1403. <page>
  1404. <font size=18>Figure 11.22  Left-justification and right-justification.</font><br>
  1405. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1406. <param  name="file" value="code/ch11/fig11_22.txt">
  1407. </applet><br>
  1408. <br>
  1409. <foreign  name="copy2disk" url="!jcpy Source/fig11_22.jar">
  1410. <foreign  name="audio" url="~audio/Ch11/11fig022.au">
  1411. <foreign  name="execute" url="!jarexe Run/fig11_22.jar">
  1412. <br>
  1413.  
  1414. </page>
  1415. <page>
  1416. <font size=18><b>Figure 11.23  Printing an integer with internal spacing and forcing the plus sign.
  1417. </b></font><br>
  1418. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1419. <param  name="file" value="code/ch11/fig11_23.txt">
  1420. </applet><br>
  1421. <br>
  1422. <foreign  name="copy2disk" url="!jcpy Source/fig11_23.jar">
  1423. <foreign  name="audio" url="~audio/Ch11/11fig023.au">
  1424. <foreign  name="execute" url="!jarexe Run/fig11_23.jar">
  1425. <br>
  1426.  
  1427. </page>
  1428. <page>
  1429. <font size=18>Figure 11.24  Using the <b>fill</b> member function and the <b>setfill</b> manipulator to change 
  1430. the padding character for fields larger than the values being printed.</font><br>
  1431. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1432. <param  name="file" value="code/ch11/fig11_24.txt">
  1433. </applet><br>
  1434. <br>
  1435. <foreign  name="copy2disk" url="!jcpy Source/fig11_24.jar">
  1436. <foreign  name="audio" url="~audio/Ch11/11fig024.au">
  1437. <foreign  name="execute" url="!jarexe Run/fig11_24.jar">
  1438. <br>
  1439.  
  1440. </page>
  1441. <page>
  1442. <font size=18>Figure 11.25  Using the <b>ios::showbase</b> flag.</font><br>
  1443. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1444. <param  name="file" value="code/ch11/fig11_25.txt">
  1445. </applet><br>
  1446. <br>
  1447. <foreign  name="copy2disk" url="!jcpy Source/fig11_25.jar">
  1448. <foreign  name="audio" url="~audio/Ch11/11fig025.au">
  1449. <foreign  name="execute" url="!jarexe Run/fig11_25.jar">
  1450. <br>
  1451.  
  1452. </page>
  1453. <page>
  1454. <font size=18>Figure 11.26  Displaying floating-point values in system default, scientific, and 
  1455. fixed formats.</font><br>
  1456. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1457. <param  name="file" value="code/ch11/fig11_26.txt">
  1458. </applet><br>
  1459. <br>
  1460. <foreign  name="copy2disk" url="!jcpy Source/fig11_26.jar">
  1461. <foreign  name="audio" url="~audio/Ch11/11fig026.au">
  1462. <foreign  name="execute" url="!jarexe Run/fig11_26.jar">
  1463. <br>
  1464.  
  1465. </page>
  1466. <page>
  1467. <font size=18>Figure 11.27  Using the <b>ios::uppercase</b> flag.</font><br>
  1468. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1469. <param  name="file" value="code/ch11/fig11_27.txt">
  1470. </applet><br>
  1471. <br>
  1472. <foreign  name="copy2disk" url="!jcpy Source/fig11_27.jar">
  1473. <foreign  name="audio" url="~audio/Ch11/11fig027.au">
  1474. <foreign  name="execute" url="!jarexe Run/fig11_27.jar">
  1475. <br>
  1476.  
  1477. </page>
  1478. <page>
  1479. <font size=18>Figure 11.28  Demonstrating the <b>flags</b> member function.</font><br>
  1480. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1481. <param  name="file" value="code/ch11/fig11_28.txt">
  1482. </applet><br>
  1483. <br>
  1484. <foreign  name="copy2disk" url="!jcpy Source/fig11_28.jar">
  1485. <foreign  name="audio" url="~audio/Ch11/11fig028.au">
  1486. <foreign  name="execute" url="!jarexe Run/fig11_28.jar">
  1487. <br>
  1488.  
  1489. </page>
  1490. <page>
  1491. <font size=18>Figure 11.29  Testing error states.</font><br>
  1492. <applet code=gsl.win.helper.TextBox width=580 height=300>
  1493. <param  name="file" value="code/ch11/fig11_29.txt">
  1494. </applet><br>
  1495. <br>
  1496. <foreign  name="copy2disk" url="!jcpy Source/fig11_29.jar">
  1497. <foreign  name="audio" url="~audio/Ch11/11fig029.au">
  1498. <foreign  name="execute" url="!jarexe Run/fig11_29.jar">
  1499. <br>
  1500.  
  1501. </page>
  1502. </section>
  1503. <section type=Popup name=Perform title="Performance">
  1504. <page>
  1505. Use unformatted I/O 
  1506. for the best 
  1507. performance in high-
  1508. volume file processing.<br>
  1509. <br>
  1510.  
  1511. </page>
  1512. </section>
  1513. <section type=Popup name=Practice title="Good Practices">
  1514. <page>
  1515. Use the C++ form of I/
  1516. O exclusively in C++ 
  1517. programs, despite the 
  1518. fact that C-style I/O is 
  1519. available to C++ 
  1520. programmers.<br>
  1521. <br>
  1522.  
  1523. </page>
  1524. <page>
  1525. When outputting 
  1526. expressions, place them 
  1527. in parentheses to 
  1528. prevent operator 
  1529. precedence problems 
  1530. between the operators 
  1531. in the expression and 
  1532. the <b><<</b> operator.<br>
  1533. <br>
  1534.  
  1535. </page>
  1536. </section>
  1537. <section type=Popup name=Objective title="Objectives">
  1538. <page>
  1539. <indent width=8 delay>*   To understand how to use C++ object-oriented stream input/output.</indent>
  1540. <indent width=8 delay>*   To be able to format inputs and outputs.</indent>
  1541. <indent width=8 delay>*   To understand the stream I/O class hierarchy.</indent>
  1542. <indent width=8 delay>*   To understand how to input/output objects of user-defined types.</indent>
  1543. <indent width=8 delay>*   To be able to create user-defined stream manipulators.</indent>
  1544. <foreign  name="audio" url="~audio/Ch11/11obj.au">
  1545.  
  1546. </page>
  1547. <page>
  1548. <indent width=8 delay>*   To be able to determine the success or failure of input/output operations.</indent>
  1549. <indent width=8 delay>*   To be able to tie output streams to input streams.</indent>
  1550. <br>
  1551.  
  1552. </page>
  1553. </section>
  1554. <section type=Popup name=Errors title="Common Errors">
  1555. <page>
  1556. Attempting to read 
  1557. from an ostream (or 
  1558. from any other output-
  1559. only stream).<br>
  1560. <br>
  1561.  
  1562. </page>
  1563. <page>
  1564. Attempting to write to 
  1565. an istream (or to any 
  1566. other input-only 
  1567. stream). <br>
  1568. <br>
  1569.  
  1570. </page>
  1571. <page>
  1572. Not providing 
  1573. parentheses to force 
  1574. proper precedence 
  1575. when using the 
  1576. relatively high 
  1577. precedence stream-
  1578. insertion operator << or 
  1579. stream-extraction 
  1580. operator >>.<br>
  1581.  
  1582. </page>
  1583. <page>
  1584. The width setting 
  1585. applies only for the 
  1586. next insertion or 
  1587. extraction; afterward, 
  1588. the width is implicitly 
  1589. set to <b>0</b>, i.e., output 
  1590. values will simply be as 
  1591. wide as they need to be. 
  1592. The <b>width</b> function 
  1593. with no argument <br>
  1594.  
  1595. </page>
  1596. <page>
  1597. returns the current 
  1598. setting. It is a logic 
  1599. error to assume that the 
  1600. width setting applies to 
  1601. all outputs.<br>
  1602. <br>
  1603.  
  1604. </page>
  1605. <page>
  1606. When not providing a 
  1607. sufficiently wide field 
  1608. to handle outputs, the 
  1609. outputs print as wide as 
  1610. they need to be, 
  1611. possibly causing 
  1612. difficult-to-read 
  1613. outputs.<br>
  1614. <br>
  1615.  
  1616. </page>
  1617. </section>
  1618. <section type=Popup name=Engineer title="Engineering">
  1619. <page>
  1620. C++ style I/O is type 
  1621. safe. <br>
  1622. <br>
  1623.  
  1624. </page>
  1625. <page>
  1626. C++ enables a common 
  1627. treatment of I/O of 
  1628. predefined types and 
  1629. user-defined types. 
  1630. This kind of 
  1631. commonality facilitates 
  1632. software development 
  1633. in general and software 
  1634. reuse in particular.<br>
  1635. <br>
  1636.  
  1637. </page>
  1638. </section>
  1639. <section type=Body name=Default title="11 C++ Stream Input/Output">
  1640. <page>
  1641. <font size=18 bold>11 C++ Stream Input/Output</font><hr>
  1642. <a href="#s1p0">11.1<spacer width=20 height=1>Introduction</a>  <br>
  1643. <a href="#s2p0">11.2<spacer width=20 height=1>Streams</a>  <br>
  1644. <a href="#s3p0">11.3<spacer width=20 height=1>Stream Output</a>  <br>
  1645. <a href="#s4p0">11.4<spacer width=20 height=1>Stream Input</a>  <br>
  1646. <a href="#s5p0">11.5<spacer width=20 height=1>Unformatted I/O with <b>read</b>, <b>gcount</b> and <b>write  
  1647. </b></a><br>
  1648. <a href="#s6p0">11.6<spacer width=20 height=1>Stream Manipulators</a>  <br>
  1649. <a href="#s7p0">11.7<spacer width=20 height=1>Stream Format States</a>  <br>
  1650. <a href="#s8p0">11.8<spacer width=20 height=1>Stream Error States</a>  <br>
  1651. <a href="#s9p0">11.9<spacer width=20 height=1>Tying an Output Stream to an Input Stream</a>  <br>
  1652. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  1653. <foreign  name="quotes" url="^Quotes::c:s0p0">
  1654.  
  1655. </page>
  1656. <page>
  1657. <a href="#s10p0">11.10<spacer width=20 height=1>Summary</a>  <br>
  1658. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  1659. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  1660. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  1661. <foreign  name="quotes" url="^Quotes::c:s0p0">
  1662.  
  1663. </page>
  1664. </section>
  1665. <section type=Body name=Default title="11.1 Introduction">
  1666. <page>
  1667. <font size=18 bold>11.1 Introduction</font><hr>
  1668. The C++ standard libraries provide an extensive set of 
  1669. input/output capabilities. This chapter discusses a range 
  1670. of capabilities sufficient for performing most common 
  1671. I/O operations and overviews the remaining 
  1672. capabilities. Some of the features presented here were 
  1673. discussed earlier in the text, but this chapter provides a 
  1674. more complete discussion of the input/output 
  1675. capabilities of C++.<br>
  1676. <spacer width=16 height=1>Many of the I/O features described here are object-
  1677. oriented. The reader should find it interesting to see 
  1678. how such capabilities are implemented. This style of I/<br>
  1679.  
  1680. </page>
  1681. <page>
  1682. O makes use of other C++ features such as references, 
  1683. function overloading, and operator overloading.<br>
  1684. <spacer width=16 height=1>As we will see,  <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>C++ uses <i>type safe I/O</i>. Each I/O 
  1685. operation is automatically performed in a manner 
  1686. sensitive to the data type. If an  <a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>I/O function has been 
  1687. properly defined to handle a particular data type, then 
  1688. that function is automatically called to handle that data 
  1689. type. If there is no match between the type of the actual 
  1690. data and a function for handling that data type, a 
  1691. compiler error indication is set. Thus, improper data 
  1692. cannot sneak through the system (as can occur in C--a 
  1693. hole in C that allows for some rather subtle and often 
  1694. bizarre errors).<br>
  1695.  
  1696. </page>
  1697. <page>
  1698. Users may specify I/O of <a href="^Engineer::c:s0p1"> <img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>user-defined types as well as 
  1699. standard types. This <i>extensibility</i> is one of the most 
  1700. valuable features of C++.<br>
  1701.  
  1702. </page>
  1703. </section>
  1704. <section type=Body name=Default title="11.2 Streams">
  1705. <page>
  1706. <font size=18 bold>11.2 Streams</font><hr>
  1707. C++ I/O occurs in <i>streams</i> of bytes. A stream is simply 
  1708. a sequence of bytes. In input operations, the bytes flow 
  1709. from a device (e.g., a keyboard, a disk drive, a network 
  1710. connection) to main memory. In output operations, 
  1711. bytes flow from main memory to a device (e.g., a 
  1712. display screen, a printer, a disk drive, a network 
  1713. connection).<br>
  1714. <spacer width=16 height=1>The application associates meaning with bytes. The 
  1715. bytes may represent ASCII characters, internal format 
  1716. raw data, graphics images, digital speech, digital video <br>
  1717.  
  1718. </page>
  1719. <page>
  1720. or any other kind of information an application may 
  1721. require.<br>
  1722. <spacer width=16 height=1>The job of the system I/O mechanisms is to move bytes 
  1723. from devices to memory and vice versa in a consistent 
  1724. and reliable manner. Such transfers often involve 
  1725. mechanical motion such as the rotation of a disk or a 
  1726. tape, or typing keystrokes at a keyboard. The time these 
  1727. transfers take is normally huge compared to the time the 
  1728. processor takes to manipulate data internally. Thus, I/O 
  1729. operations require careful planning and tuning to ensure 
  1730. maximum performance. Issues like this are discussed in 
  1731. depth in operating systems textbooks (De90).<br>
  1732.  
  1733. </page>
  1734. <page>
  1735. C++ provides both "low-level" and "high-level" I/O 
  1736. capabilities. Low-level I/O capabilities (i.e., 
  1737. unformatted I/O) typically specify that some number of 
  1738. bytes should simply be transferred device-to-memory 
  1739. or memory-to-device. In such transfers, the individual 
  1740. byte is the item of interest. Such low-level capabilities 
  1741. do provide high-speed, high-volume transfers, but these 
  1742. capabilities are not particularly convenient for people.<br>
  1743. <spacer width=16 height=1>People prefer a higher-level view of I/O, i.e., <i>formatted 
  1744. I/O</i>, in which bytes are grouped into meaningful units 
  1745. such as integers, floating-point numbers, characters, 
  1746. strings and user-defined types. These type-oriented <br>
  1747.  
  1748. </page>
  1749. <page>
  1750. capabilities are satisfactory for most I/O other than  
  1751. high-volume file  <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>processing.<br>
  1752.  
  1753. </page>
  1754. <page>
  1755. <b>Drag the correct term to the box associated with the 
  1756. attribute:</b><br>
  1757. <component type="drag" width=72 height=18 label="low-level" name="low-level">   <component type="drag" width=80 height=18 label="high-level" name="high-level">   <component type="drag" width=48 height=18 label="stream" name="stream">   <br>
  1758. Sequence of bytes.<component type="drop" width=88 height=18 name="stream">  <br>
  1759. Unformatted, byte-based input/output.<component type="drop" width=88 height=18 name="low-level">  <br>
  1760. Formatted, data-type oriented input/output.<component type="drop" width=88 height=18 name="high-level">  <br>
  1761. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1762.  
  1763. </page>
  1764. <page pagename="11.2.1  Iostream Library Header Files">
  1765. <b>11.2.1  Iostream Library Header Files</b><br>
  1766. The C++ <b>iostream</b> library provides hundreds of I/O 
  1767. capabilities. Several header files contain portions of the 
  1768. library interface. <br>
  1769. <spacer width=16 height=1>Most C++ programs include the <b><iostream.h></b> header 
  1770. file which contains basic information required for all 
  1771. stream-I/O operations. The <b><iostream.h></b> header file 
  1772. contains the <b>cin</b>, <b>cout</b>, <b>cerr</b>, and <b>clog</b> objects which 
  1773. correspond to the standard input stream, standard 
  1774. output stream, the unbuffered standard error stream, 
  1775. and the buffered standard error stream. Both 
  1776. unformatted- and formatted-I/O capabilities are 
  1777. provided.<br>
  1778.  
  1779. </page>
  1780. <page pagename="11.2.1  Iostream Library Header Files">
  1781. The <b><iomanip.h> </b>header contains information useful 
  1782. for performing formatted I/O with so-called 
  1783. <i>parameterized stream manipulators</i>.<br>
  1784. <spacer width=16 height=1>The <b><fstream.h></b> header contains information 
  1785. important for user-controlled file processing operations. 
  1786. We use this header in the file processing programs of 
  1787. Chapter 13.<br>
  1788. <spacer width=16 height=1>Each C++ implementation generally contains other I/O-
  1789. related libraries that provide system-specific 
  1790. capabilities such as controlling special-purpose devices 
  1791. for audio and video I/O.<br>
  1792.  
  1793. </page>
  1794. <page pagename="11.2.1  Iostream Library Header Files">
  1795. <b>Select the true statement(s). </b><br>
  1796. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. This header provides C++'s file processing capabilities.">
  1797. Header <fstream.h> provides I/O formatting capabilities.   <br>
  1798. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1799. clog is an object representing a buffered error stream. <br>
  1800. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1801. The header <iomanip.h> contains parameterized stream manipulators. <br>
  1802. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1803.  
  1804. </page>
  1805. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1806. <b>11.2.2   Stream Input/Output Classes and Objects</b><br>
  1807. The <b>iostream</b> library contains many classes for 
  1808. handling a wide variety of I/O operations. The <b>istream</b> 
  1809. class supports stream-input operations. The <b>ostream</b> 
  1810. class supports stream-output operations. The <b>iostream</b> 
  1811. class supports both stream-input and stream-output 
  1812. operations. <br>
  1813. <spacer width=16 height=1>The <b>istream</b> class and the <b>ostream</b> class are each 
  1814. derived through single inheritance from the <b>ios</b> base 
  1815. class. The <b>iostream</b> class is derived through multiple 
  1816. inheritance from both the <b>istream</b> class and the 
  1817. <b>ostream</b> class. These inheritance relationships are 
  1818. summarized in  <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 11.1</a>. <br>
  1819.  
  1820. </page>
  1821. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1822. Operator overloading provides a convenient notation 
  1823. for performing input/output. The left shift operator (<b><<</b>) 
  1824. is overloaded to designate stream output and is referred 
  1825. to as the <i>stream-insertion operator</i>. The right shift 
  1826. operator (<b>>></b>) is overloaded to designate stream input 
  1827. and is referred to as the <i>stream-extraction operator</i>. 
  1828. These operators are used with the standard stream 
  1829. objects <b>cin</b>, <b>cout</b>, <b>cerr</b>, and <b>clog</b>, and commonly with 
  1830. user-defined stream objects.<br>
  1831. <spacer width=16 height=1><b>cin</b> is an object of the <b>istream</b> class and is said to be 
  1832. "tied to" (or connected to) the standard input device, 
  1833. normally the keyboard. The stream-extraction operator 
  1834. as used in the following statement causes a value for <br>
  1835.  
  1836. </page>
  1837. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1838. integer variable <b>grade</b> (assuming <b>grade</b> has been 
  1839. declared as <b>int</b>) to be input from <b>cin</b> to memory:<br>
  1840. <font size=2><br></font><font size=11><pre>
  1841. cin >> grade;<p>
  1842. </pre></font>
  1843. Note that the stream-extraction operation is "smart 
  1844. enough" to "know" what the type of the data is. 
  1845. Assuming that <b>grade</b> has been properly declared, no 
  1846. additional type information needs to be specified for 
  1847. use with the stream-extraction operator (as is the case, 
  1848. incidentally, in C-style I/O).<br>
  1849. <spacer width=16 height=1><b>cout</b> is an object of the <b>ostream</b> class and is said to be 
  1850. "tied to" the standard output device, normally the 
  1851. display screen. The stream-insertion operator as used in 
  1852. the following statement causes the value of integer <br>
  1853.  
  1854. </page>
  1855. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1856. variable <b>grade</b> to be output from memory to the 
  1857. standard output device:<br>
  1858. <font size=2><br></font><font size=11><pre>
  1859. cout << grade;<p>
  1860. </pre></font>
  1861. Note that the stream-insertion operator is "smart 
  1862. enough" to "know" the type of <b>grade</b> (assuming it has 
  1863. been properly declared), so no additional type 
  1864. information needs to be specified for use with the 
  1865. stream-insertion operator.<br>
  1866. <spacer width=16 height=1><b>cerr</b> is an object of the <b>ostream</b> class and is said to be 
  1867. "tied to" the standard error device. Outputs to object 
  1868. <b>cerr</b> are unbuffered. This means that each stream 
  1869. insertion to <b>cerr</b> causes its output to appear <br>
  1870.  
  1871. </page>
  1872. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1873. immediately; this is appropriate for promptly notifying 
  1874. a user about errors. <br>
  1875. <spacer width=16 height=1><b>clog</b> is an object of the <b>ostream</b> class and is also said to 
  1876. be "tied to" the standard error device. Outputs to <b>clog</b> 
  1877. are buffered. This means that each insertion to <b>clog</b> 
  1878. could cause its output to be held in a buffer until the 
  1879. buffer is filled or until the buffer is flushed. <br>
  1880. C++ file processing uses the classes <b>ifstream</b> to 
  1881. perform file input operations, <b>ofstream</b> for file output 
  1882. operations, and <b>fstream</b> for file input/output operations. 
  1883. The <b>ifstream</b> class inherits from <b>istream</b>, the <b>ofstream</b> 
  1884. class inherits from <b>ostream</b>, and the <b>fstream</b> class 
  1885. inherits from <b>iostream</b>. The various inheritance <br>
  1886.  
  1887. </page>
  1888. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1889. relationships of the I/O-related classes are summarized 
  1890. in  <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 11.2</a>. There are many more classes in the full 
  1891. stream-I/O class hierarchy supported at most 
  1892. installations, but the classes shown here provide the 
  1893. vast majority of the capabilities most programmers will 
  1894. need. See the class library reference for your C++ 
  1895. system for more file processing information.<br>
  1896.  
  1897. </page>
  1898. <page pagename="11.2.2   Stream Input/Output Classes and Objects">
  1899. <b>Drag the correct term to the box associated with the 
  1900. attribute:</b><br>
  1901. <component type="drag" width=56 height=18 label="istream" name="istream">   <component type="drag" width=56 height=18 label="ostream" name="ostream">   <component type="drag" width=24 height=18 label="ios" name="ios">   <component type="drag" width=64 height=18 label="ifstream" name="ifstream">   <component type="drag" width=64 height=18 label="ofstream" name="ofstream">   <component type="drag" width=56 height=18 label="fstream" name="fstream">   <br>
  1902. Data type for file output. <component type="drop" width=72 height=18 name="ofstream">  <br>
  1903. Base class of istream. <component type="drop" width=72 height=18 name="ios">  <br>
  1904. Data type for both file output and file input.<component type="drop" width=72 height=18 name="fstream">  <br>
  1905. Class type for cerr and clog.<component type="drop" width=72 height=18 name="ostream">  <br>
  1906. File input operations.<component type="drop" width=72 height=18 name="ifstream">  <br>
  1907. Stream input operations.<component type="drop" width=72 height=18 name="istream">  <br>
  1908. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1909.  
  1910. </page>
  1911. </section>
  1912. <section type=Body name=Default title="11.3 Stream Output">
  1913. <page>
  1914. <font size=18 bold>11.3 Stream Output</font><hr>
  1915. The C++ <b>ostream</b> class provides the ability to perform 
  1916. formatted and unformatted output. Capabilities for 
  1917. output include: output of standard data types with the 
  1918. stream-insertion operator; output of characters with the 
  1919. <b>put</b> member function; unformatted output with the 
  1920. <b>write</b> member function (<a href="#s5p0">Section 11.5</a>); output of 
  1921. integers in decimal, octal and hexadecimal formats 
  1922. (<a href="#s6p1">Section 11.6.1</a>); output of floating-point values with 
  1923. various precisions (<a href="#s6p3">Section 11.6.2</a>), with forced decimal 
  1924. points (<a href="#s7p5">Section 11.7.2</a>), in scientific notation and in 
  1925. fixed notation (<a href="#s7p13">Section 11.7.6</a>); output of data justified <br>
  1926.  
  1927. </page>
  1928. <page>
  1929. in fields of designated field widths (<a href="#s7p6">Section 11.7.3</a>); 
  1930. output of data in fields padded with specified characters 
  1931. (<a href="#s7p9">Section 11.7.4</a>); and output of uppercase letters in 
  1932. scientific notation and hexadecimal notation (<a href="#s7p15">Section 
  1933. 11.7.7</a>).<br>
  1934.  
  1935. </page>
  1936. <page pagename="11.3.1   Stream-Insertion Operator">
  1937. <b>11.3.1   Stream-Insertion Operator</b><br>
  1938. Stream output may be performed with the stream-
  1939. insertion operator, i.e., the overloaded << operator. The 
  1940. << operator is overloaded to output data items of built-
  1941. in types, to output strings and to output pointer values. 
  1942. <a href="#s9p0">Section 11.9</a> shows how to overload << to output data 
  1943. items of user-defined types.  <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.3</a> demonstrates 
  1944. output of a string using a single stream-insertion 
  1945. statement. Multiple insertion statements may be used as 
  1946. in  <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.4</a>. When this program is run, it produces the 
  1947. same output as the previous program.<br>
  1948. <spacer width=16 height=1>The effect of the <b>\n</b> (newline) escape sequence is also 
  1949. achieved by the <b>endl</b> (end line) <i>stream manipulator</i>, as <br>
  1950.  
  1951. </page>
  1952. <page pagename="11.3.1   Stream-Insertion Operator">
  1953. in <a href="^Code::c:s0p2"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.5</a>. The <b>endl</b> stream manipulator issues a 
  1954. newline character and, in addition, flushes the  <a href="^Practice::c:s0p1"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>output 
  1955. buffer (i.e., causes the output buffer to be output 
  1956. immediately even if it is not full). The output buffer 
  1957. may also be flushed simply by <br>
  1958. <font size=2><br></font><font size=11><pre>
  1959. cout << flush;<p>
  1960. </pre></font>
  1961. Stream manipulators are discussed in detail in <a href="#s6p0">Section 
  1962. 11.6</a>.<br>
  1963. <spacer width=16 height=1>Expressions can be output as shown in  <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.6</a>.<br>
  1964.  
  1965. </page>
  1966. <page pagename="11.3.1   Stream-Insertion Operator">
  1967. <b>Select the statement that is equivalent to<p>
  1968. cout << "Hello" << endl;. </b><br>
  1969. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1970. cout << "Hello\n";.    <br>
  1971. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1972. cout << "Hello\n\n";.   <br>
  1973. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  1974. cout << "Hello\n" << flush.  <br>
  1975. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1976. cout << "Hello" << flush.  <br>
  1977. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1978.  
  1979. </page>
  1980. <page pagename="11.3.2   Cascading Stream-Insertion/Extraction ">
  1981. Operators
  1982. <b>11.3.2   Cascading Stream-Insertion/Extraction 
  1983. Operators</b><br>
  1984. The overloaded <b><<</b> and <b>>></b> operators may each be used 
  1985. in a <i>cascaded form</i> as shown in <a href="^Code::c:s0p4"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.7</a>.<br>
  1986. <spacer width=16 height=1>The multiple stream insertions in  <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.7</a> are 
  1987. executed as if they had been written:<br>
  1988. <font size=2><br></font><font size=11><pre>
  1989. ( ( ( cout << "47 plus 53 is " ) <p>
  1990.     << ( 47 + 53 ) ) << endl );<p>
  1991. </pre></font>
  1992. i.e., <b><<</b> associates from left to right. This kind of 
  1993. cascading of stream-insertion operators is allowed 
  1994. because the overloaded <b><<</b> operator returns a reference 
  1995. to its left-operand object, i.e., <b>cout</b>. Thus the leftmost 
  1996. parenthesized expression<br>
  1997.  
  1998. </page>
  1999. <page pagename="11.3.2   Cascading Stream-Insertion/Extraction ">
  2000. <font size=2><br></font><font size=11><pre>
  2001. ( cout << "47 plus 53 is " )<p>
  2002. </pre></font>
  2003. outputs the specified character string and returns a 
  2004. reference to <b>cout</b>. This allows the middle parenthesized 
  2005. expression to be evaluated as<br>
  2006. <font size=2><br></font><font size=11><pre>
  2007. ( cout << ( 47 + 53 ) )<p>
  2008. </pre></font>
  2009. which outputs the integer value <b>100</b> and returns a 
  2010. reference to <b>cout</b>. The rightmost parenthesized 
  2011. expression is then evaluated as<br>
  2012. <font size=2><br></font><font size=11><pre>
  2013. cout << endl<p>
  2014. </pre></font>
  2015. which outputs a newline, flushes <b>cout</b>, and returns a 
  2016. reference to <b>cout</b>. This last return is not used. <br>
  2017.  
  2018. </page>
  2019. <page pagename="11.3.2   Cascading Stream-Insertion/Extraction ">
  2020. <b>Select the statement that results in a syntax error. </b><br>
  2021. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2022. cout << ( a + b / 8 );.    <br>
  2023. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2024. cout << ( ( x ) );.   <br>
  2025. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2026. cout << "Hello" << ' ' << endl.  <br>
  2027. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2028. cout << z += 22;.  <br>
  2029. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2030.  
  2031. </page>
  2032. <page pagename="11.3.3  Output of <b>char *</b> Variables">
  2033. <b>11.3.3  Output of <b>char *</b> Variables</b><br>
  2034. In C-style I/O, it is necessary for the programmer to 
  2035. supply type information. C++ determines data types 
  2036. automatically--a nice improvement over <b>C</b>. But 
  2037. sometimes this "gets in the way." For example, we 
  2038. know that a character string is of type <b>char *</b>. Suppose 
  2039. we want to print the value of that pointer, i.e., the 
  2040. memory address of the first character of that string. But 
  2041. the <b><<</b> operator has been overloaded to print data of 
  2042. type <b>char *</b> as a null-terminated string. The solution is 
  2043. to cast the pointer to <b>void *</b> (this should be done to any 
  2044. pointer variable the programmer wishes to output as an 
  2045. address).  <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.8</a> demonstrates printing a <b>char *</b> <br>
  2046.  
  2047. </page>
  2048. <page pagename="11.3.3  Output of <b>char *</b> Variables">
  2049. variable in both string and address formats. Note that 
  2050. the address prints as a hexadecimal (base 16) number. 
  2051. Hexadecimal numbers in C++ begin with <b>0x</b> or <b>0X</b>. We 
  2052. say more about controlling the bases of numbers in 
  2053. <a href="#s6p1">Sections 11.6.1</a>,<a href="#s7p9"> 11.7.4</a>, <a href="#s7p11">11.7.5</a> and <a href="#s7p15">11.7.7</a>.<br>
  2054.  
  2055. </page>
  2056. <page pagename="11.3.3  Output of <b>char *</b> Variables">
  2057. <b>Select the statement that outputs the address 
  2058. pointed to by char *ptr. </b><br>
  2059. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2060. cout << *ptr;.    <br>
  2061. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  2062. cout << static_cast< void * >( ptr );.  <br>
  2063. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2064. cout << static_cast< void * >( *ptr );.   <br>
  2065. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2066. cout << ptr;.  <br>
  2067. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2068.  
  2069. </page>
  2070. <page pagename="11.3.4   Character Output with Member Function ">
  2071. <b>put</b>; Cascading <b>put</b>s
  2072. <b>11.3.4   Character Output with Member Function 
  2073. <b>put</b>; Cascading <b>put</b>s</b><br>
  2074. The <b>put</b> member function outputs one character as in<br>
  2075. <font size=2><br></font><font size=11><pre>
  2076. cout.put( 'A' );<p>
  2077. </pre></font>
  2078. which displays <b>A</b> on the screen. Calls to <b>put</b> may be 
  2079. cascaded as in<br>
  2080. <font size=2><br></font><font size=11><pre>
  2081. cout.put( 'A' ).put( '\n' );<p>
  2082. </pre></font>
  2083. which outputs the letter <b>A</b> followed by a newline 
  2084. character. As with <b><<</b>, the preceding statement executes 
  2085. in this manner because the dot operator (<b>.</b>) associates 
  2086. from left to right and the <b>put</b> member function returns a 
  2087. reference to the object through which the <b>put</b> call is <br>
  2088.  
  2089. </page>
  2090. <page pagename="11.3.4   Character Output with Member Function ">
  2091. invoked. The put function may also be called with an 
  2092. ASCII-valued expression, as in <b>cout.put( 65 )</b> which 
  2093. also outputs <b>A</b>.<br>
  2094.  
  2095. </page>
  2096. <page pagename="11.3.4   Character Output with Member Function ">
  2097. <b>Select the statement that generates a syntax error. </b><br>
  2098. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2099. cout.put( 'x' );.    <br>
  2100. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2101. cout.put( "Hello" );.  <br>
  2102. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2103. cout.put( '\n' );.   <br>
  2104. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2105. cout.put( 'a' ).put( 'b' );.  <br>
  2106. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2107.  
  2108. </page>
  2109. </section>
  2110. <section type=Body name=Default title="11.4 Stream Input">
  2111. <page>
  2112. <font size=18 bold>11.4 Stream Input</font><hr>
  2113. Now let us consider stream input. This may be 
  2114. performed with the stream-extraction operator, i.e., the 
  2115. overloaded <b>>></b> operator. This operator normally skips 
  2116. <i>whitespace characters</i> (such as blanks, tabs, and 
  2117. newlines) in the input stream. Later we will see how to 
  2118. change this behavior. The stream-extraction operator 
  2119. returns zero (false) when end-of-file is encountered on a 
  2120. stream; otherwise, the stream-extraction operator 
  2121. returns a reference to the object through which it is 
  2122. invoked. Each stream contains a set of <i>state bits</i> used to 
  2123. control the state of the stream (i.e., formatting, setting <br>
  2124.  
  2125. </page>
  2126. <page>
  2127. error states, etc.). Stream extraction causes the stream's 
  2128. <b>failbit</b> to be set if data of the wrong type is input, and 
  2129. causes the stream's <b>badbit</b> to be set if the operation 
  2130. fails. We will soon see how to test these bits after an I/O 
  2131. operation. <a href="#s7p0">Sections 11.7</a> and<a href="#s8p0"> 11.8</a> discuss the stream 
  2132. state bits in detail.<br>
  2133.  
  2134. </page>
  2135. <page pagename="11.4.1   Stream-Extraction Operator">
  2136. <b>11.4.1   Stream-Extraction Operator</b><br>
  2137. To  <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>read two integers, use the <b>cin</b> object and the 
  2138. overloaded <b>>></b>  <a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>stream-extraction operator as in <a href="^Code::c:s0p6"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 
  2139. 11.9</a>. Note that stream-extraction operations can also be 
  2140. cascaded.<br>
  2141. <spacer width=16 height=1>The <a href="^Errors::c:s0p2"> <img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>relatively high precedence of the <b>>></b> and <b><<</b> 
  2142. operators can cause problems. For example, the 
  2143. program of  <a href="^Code::c:s0p7"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.10</a> will not compile properly 
  2144. without the parentheses around the conditional 
  2145. expression. The reader should verify this. <br>
  2146. <spacer width=16 height=1>One popular way to <a href="^Portable::c:s0p0"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>input a series of values is to use the 
  2147. stream-extraction operation in the loop-continuation 
  2148. condition of a <b>while</b> loop. The extraction returns false <br>
  2149.  
  2150. </page>
  2151. <page pagename="11.4.1   Stream-Extraction Operator">
  2152. (0) when end-of-file is encountered. Consider the 
  2153. program of  <a href="^Code::c:s0p8"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.11</a> which finds the highest grade on 
  2154. an exam. Assume the number of grades is not known in 
  2155. advance and that the user will type end-of-file to 
  2156. indicate that all the grades have been entered. The 
  2157. <b>while</b> condition, <b>(cin >> grade )</b>, becomes 0 
  2158. (interpreted as <tt><b>false</b></tt>) when the user enters end-of-
  2159. file. <br>
  2160. <spacer width=16 height=1>In <a href="^Code::c:s0p8"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.11</a>, <b>cin >> grade</b> can be used as a condition 
  2161. because the base class <b>ios</b> (from which <b>istream</b> is 
  2162. inherited) provides an overloaded cast operator that 
  2163. converts a stream into a pointer of type <b>void *</b>. The 
  2164. value of the pointer is 0 if an error occurred while <br>
  2165.  
  2166. </page>
  2167. <page pagename="11.4.1   Stream-Extraction Operator">
  2168. attempting to read a value or the end-of-file indicator 
  2169. was encountered. The compiler is able to use the <b>void *</b> 
  2170. cast operator implicitly.<br>
  2171.  
  2172. </page>
  2173. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2174. <b>11.4.2   <b>get</b> and <b>getline</b> Member Functions</b><br>
  2175. The <b>get</b> member function with no arguments inputs one 
  2176. character from the designated stream (even if this is 
  2177. whitespace) and returns this character as the value of 
  2178. the function call. This version of <b>get</b> returns <b>EOF</b> when 
  2179. end-of-file on the stream is encountered. <br>
  2180. <spacer width=16 height=1> <a href="^Code::c:s0p9"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.12</a> demonstrates the use of member functions 
  2181. <b>eof</b> and <b>get</b> on input stream <b>cin</b> and member function 
  2182. <b>put</b> on output stream <b>cout</b>. The program first prints the 
  2183. value of <b>cin.eof()</b>, i.e., <b>false</b> (<tt><b>0</b></tt> on the output) to show 
  2184. that end-of-file has not occurred on <b>cin</b>. The user enters 
  2185. a line of text and presses <i>Enter</i> followed by end-of-file 
  2186. (<i><ctrl>-z</i> on IBM PC-compatible systems, <i><ctrl>-d</i> on <br>
  2187.  
  2188. </page>
  2189. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2190. UNIX and Macintosh systems). The program reads 
  2191. each character and outputs it to <b>cout</b> using member 
  2192. function <b>put</b>. When the end-of-file is encountered, the 
  2193. <b>while</b> ends and <b>cin.eof()</b>--now <b>true</b>--is printed again 
  2194. (<tt><b>1</b></tt> on the output) to show that end-of-file has been set 
  2195. on <b>cin</b>. Note that this program uses the version of 
  2196. <b>istream</b> member function <b>get</b> that takes no arguments 
  2197. and returns the character being input.<br>
  2198. <spacer width=16 height=1>The <b>get</b> member function with a character argument 
  2199. inputs the next character from the input stream (even if 
  2200. this is a whitespace character) and stores it in the 
  2201. character argument. This version of <b>get</b> returns 0 when 
  2202. end-of-file is encountered; otherwise this version of <b>get</b> <br>
  2203.  
  2204. </page>
  2205. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2206. returns a reference to the <b>istream</b> object for which the 
  2207. get member function is being invoked. <br>
  2208. <spacer width=16 height=1>A third version of the <b>get</b> member function takes three 
  2209. arguments--a character array, a size limit, and a 
  2210. delimiter (with default value '<b>\n</b>'). This version reads 
  2211. characters from the input stream. It reads up to one less 
  2212. than the specified maximum number of characters and 
  2213. terminates, or terminates as soon as the delimiter is 
  2214. read. A null character is inserted to terminate the input 
  2215. string in the character array used as a buffer by the 
  2216. program. The delimiter is not placed in the character 
  2217. array, but does remain in the input stream (the delimiter 
  2218. will be the next character read). Thus, the result of a <br>
  2219.  
  2220. </page>
  2221. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2222. second consecutive <b>get</b> is an empty line unless the 
  2223. delimiter character is flushed from the input stream. 
  2224. <a href="^Code::c:s0p10"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.13</a> compares input using <b>cin</b> with stream 
  2225. extraction (which reads characters until a whitespace 
  2226. character is encountered) and input with <b>cin.get</b>. Note 
  2227. that the call to <b>cin.get</b> does not specify a delimiter 
  2228. character, so the default '<b>\n</b>' is used.<br>
  2229. <spacer width=16 height=1>The <b>getline</b> member function operates like the third 
  2230. version of the <b>get</b> member function and inserts a null 
  2231. character after the line in the character array. The 
  2232. <b>getline</b> function removes the delimiter from the stream 
  2233. (i.e., reads the character and discards it), but does not 
  2234. store it in the character array. The program of <br>
  2235.  
  2236. </page>
  2237. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2238.  <a href="^Code::c:s0p11"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.14</a> demonstrates the use of the <b>getline</b> member 
  2239. function to input a line of text.<br>
  2240.  
  2241. </page>
  2242. <page pagename="11.4.2   <b>get</b> and <b>getline</b> Member Functions">
  2243. <b>Select the statement that generates a syntax error. </b><br>
  2244. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2245. char c = cin.get();.   <br>
  2246. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2247. cin.getline( charArray, 88 );.  <br>
  2248. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2249. cin >> charArray;. <br>
  2250. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2251. char c[255] = cin.get();. <br>
  2252. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2253.  
  2254. </page>
  2255. <page pagename="11.4.3   istream Member Functions peek, putback ">
  2256. and ignore
  2257. <b>11.4.3   istream Member Functions peek, putback 
  2258. and ignore</b><br>
  2259. The <b>ignore</b> member function skips over a designated 
  2260. number of characters (default is one character) or 
  2261. terminates upon encountering a designated delimiter 
  2262. (the default delimiter is <b>EOF</b> which causes <b>ignore</b> to 
  2263. skip to the end of file when reading from a file).<br>
  2264. <spacer width=16 height=1>The <b>putback</b> member function places the previous 
  2265. character obtained by a <b>get</b> on an input stream back 
  2266. onto that stream. This function is useful for applications 
  2267. that scan an input stream looking for a field beginning 
  2268. with a specific character. When that character is input, 
  2269. the application puts that character back on the stream so <br>
  2270.  
  2271. </page>
  2272. <page pagename="11.4.3   istream Member Functions peek, putback ">
  2273. the character can be included in the data about to be 
  2274. input.<br>
  2275. <spacer width=16 height=1>The <b>peek</b> member function returns the next character 
  2276. from an input stream, but does not remove the character 
  2277. from the stream.<br>
  2278.  
  2279. </page>
  2280. <page pagename="11.4.4   Type-Safe I/O">
  2281. <b>11.4.4   Type-Safe I/O</b><br>
  2282. C++ offers <i>type-safe I/O</i>. The <b><<</b> and <b>>></b> operators are 
  2283. overloaded to accept data items of specific types. If 
  2284. unexpected data is processed, various error flags are set 
  2285. which the user may test to determine if an I/O operation 
  2286. succeeded or failed. In this manner the program "stays 
  2287. in control." We discuss these error flags in <a href="#s8p0">Section 11.8</a>.<br>
  2288.  
  2289. </page>
  2290. </section>
  2291. <section type=Body name=Default title="11.5 Unformatted I/O with read, gcount and write">
  2292. <page>
  2293. <font size=18 bold>11.5 Unformatted I/O with <b>read</b>, <b>gcount</b> and 
  2294. <b>write
  2295. </b></font><hr>
  2296. <i>Unformatted input/output</i> is performed with the <b>read</b> 
  2297. and <b>write</b> member functions. Each of these inputs or 
  2298. outputs some number of bytes to or from a character 
  2299. array in memory. These bytes are not formatted in any 
  2300. way. They are simply input or output as raw bytes. For 
  2301. example, the call<br>
  2302. <font size=2><br></font><font size=11><pre>
  2303. char buffer[] = "HAPPY BIRTHDAY";<p>
  2304. cout.write( buffer, 10 );<p>
  2305. </pre></font>
  2306. outputs the first 10 bytes of <b>buffer</b> (including null 
  2307. characters that would cause output with <b>cout</b> and <b><<</b> to <br>
  2308.  
  2309. </page>
  2310. <page>
  2311. terminate). Since a character string evaluates to the 
  2312. address of its first character, the call<br>
  2313. <font size=2><br></font><font size=11><pre>
  2314. cout.write( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10 );<p>
  2315. </pre></font>
  2316. displays the first 10 characters of the alphabet.<br>
  2317. <spacer width=16 height=1>The <b>read</b> member function inputs into a character array 
  2318. a designated number of characters. If fewer than the 
  2319. designated number of characters are read, <b>failbit</b> is set. 
  2320. We will soon see how to determine if <b>failbit</b> has been 
  2321. set (see <a href="#s8p0">Section 11.8</a>).<br>
  2322. <spacer width=16 height=1>The <b>gcount</b> member function reports the number of 
  2323. characters read by the last input operation. <br>
  2324. <spacer width=16 height=1> <a href="^Code::c:s0p12"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.15</a> demonstrates <b>istream</b> member functions 
  2325. <b>read</b> and <b>gcount</b> and <b>ostream</b> member function <b>write</b>. <br>
  2326.  
  2327. </page>
  2328. <page>
  2329. The program inputs 20 characters (from a longer input 
  2330. sequence) into character array <b>buffer</b> with <b>read</b>, 
  2331. determines the number of characters input with <b>gcount</b>, 
  2332. and outputs the characters in <b>buffer</b> with <b>write</b>.<br>
  2333.  
  2334. </page>
  2335. <page>
  2336. <b>Select the statement that generates a syntax error. </b><br>
  2337. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2338. cout.write( charArray, 78 );.   <br>
  2339. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2340. cout.read( charArray );. <br>
  2341. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2342. cin.read( charArray, 100 );.  <br>
  2343. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2344. cout.write( "hello", 3 );. <br>
  2345. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2346.  
  2347. </page>
  2348. </section>
  2349. <section type=Body name=Default title="11.6 Stream Manipulators">
  2350. <page>
  2351. <font size=18 bold>11.6 Stream Manipulators</font><hr>
  2352. C++ provides various <i>stream manipulators</i> that perform 
  2353. formatting tasks. The stream manipulators provide 
  2354. capabilities such as setting field widths, setting 
  2355. precisions, setting and unsetting format flags, setting 
  2356. the fill character in fields, flushing streams, inserting a 
  2357. newline in the output stream and flushing the stream, 
  2358. inserting a null character in the output stream, and 
  2359. skipping whitespace in the input stream. These features 
  2360. are described in the following sections.<br>
  2361.  
  2362. </page>
  2363. <page pagename="11.6.1   Integral Stream Base: <b>dec</b>, <b>oct</b>, <b>hex</b> and ">
  2364. <b>setbase
  2365. </b>
  2366. <b>11.6.1   Integral Stream Base: <b>dec</b>, <b>oct</b>, <b>hex</b> and 
  2367. <b>setbase
  2368. </b></b><br>
  2369. Integers are normally interpreted as decimal (base 10) 
  2370. values. To change the base in which integers are 
  2371. interpreted on a stream, insert the manipulator <b>hex</b> to 
  2372. set the base to hexadecimal (base 16) or insert the 
  2373. manipulator <b>oct</b> to set the base to octal (base 8). Insert 
  2374. the <b>dec</b> stream manipulator to reset the stream base to 
  2375. decimal. <br>
  2376. <spacer width=16 height=1>The base of a stream may also be changed by the stream 
  2377. manipulator <b>setbase</b> which takes one integer argument 
  2378. of <b>10</b>, <b>8</b>, or <b>16</b> to set the base. Because <b>setbase</b> takes an 
  2379. argument, it is called a <i>parameterized stream 
  2380. </i><br>
  2381.  
  2382. </page>
  2383. <page pagename="11.6.1   Integral Stream Base: <b>dec</b>, <b>oct</b>, <b>hex</b> and ">
  2384. <i>manipulator</i>. Using <b>setbase</b> or any other parameterized 
  2385. manipulator requires the inclusion of the <b><iomanip.h></b> 
  2386. header file. The stream base remains the same until it is 
  2387. explicitly changed. <a href="^Code::c:s0p13"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.16</a> shows the use of the 
  2388. <b>hex</b>, <b>oct</b>, <b>dec</b>, and <b>setbase</b> stream manipulators.<br>
  2389.  
  2390. </page>
  2391. <page pagename="11.6.2   Floating-Point Precision (<b>precision</b>, ">
  2392. <b>setprecision</b>)
  2393. <b>11.6.2   Floating-Point Precision (<b>precision</b>, 
  2394. <b>setprecision</b>)</b><br>
  2395. We can control the <i>precision</i> of floating-point numbers, 
  2396. i.e., the number of digits to the right of the decimal 
  2397. point, by using either the <b>setprecision</b> stream 
  2398. manipulator or the <b>precision</b> member function. A call 
  2399. to either of these sets the precision for all subsequent 
  2400. output operations until the next precision-setting call. 
  2401. The <b>precision</b> member function with no argument 
  2402. returns the current precision setting. The program of 
  2403. <a href="^Code::c:s0p14"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.17</a> uses both the <b>precision</b> member function and 
  2404. the <b>setprecision</b> manipulator to print a table showing <br>
  2405.  
  2406. </page>
  2407. <page pagename="11.6.2   Floating-Point Precision (<b>precision</b>, ">
  2408. the square root of <b>2</b> with precisions varying from <b>0</b> 
  2409. through <b>9</b>. <br>
  2410.  
  2411. </page>
  2412. <page pagename="11.6.2   Floating-Point Precision (<b>precision</b>, ">
  2413. <b>Select the statement that generates a syntax error. </b><br>
  2414. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2415. cout << bin << 1988; // output binary. <br>
  2416. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2417. cout << oct << 1987; // output octal.   <br>
  2418. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2419. cout << hex << 1986; // output hexadecimal.  <br>
  2420. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2421. cout << setprecision( 3 ) << 8.22;. <br>
  2422. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2423.  
  2424. </page>
  2425. <page pagename="11.6.3   Field Width (<b>setw</b>, <b>width</b>)">
  2426. <b>11.6.3   Field Width (<b>setw</b>, <b>width</b>)</b><br>
  2427. The <b>ios</b> <b>width</b> member function sets the field  <a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>width 
  2428. (i.e., the number of character positions in which a value 
  2429. should be output or the number of characters that 
  2430. should be input) and returns the previous width. If 
  2431. values processed are  <a href="^Errors::c:s0p5"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>smaller than the field width, <i>fill 
  2432. characters</i> are inserted as <i>padding</i>. A value wider than 
  2433. the designated width will not be truncated--the full 
  2434. number will be printed. <br>
  2435.  <a href="^Code::c:s0p15"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.18</a> demonstrates the use of the <b>width</b> 
  2436. member function on both input and output. Note that on 
  2437. input, a maximum of one fewer characters than the 
  2438. width will be read because provision is made for the <br>
  2439.  
  2440. </page>
  2441. <page pagename="11.6.3   Field Width (<b>setw</b>, <b>width</b>)">
  2442. null character to be placed in the input string. 
  2443. Remember that stream extraction terminates when 
  2444. nonleading whitespace is encountered. The <b>setw</b> stream 
  2445. manipulator also may be used to set the field width. 
  2446. Note: When the user is prompted for input, the user 
  2447. should enter a line of text and press <i>Enter</i> followed by 
  2448. end-of-file (<i><ctrl>-z</i> on IBM PC-compatible systems, 
  2449. <i><ctrl>-d</i> on UNIX and Macintosh systems). <br>
  2450.  
  2451. </page>
  2452. <page pagename="11.6.3   Field Width (<b>setw</b>, <b>width</b>)">
  2453. <b>Select the statement that generates a syntax error. </b><br>
  2454. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2455. cout << setw( 5 ) << x;.   <br>
  2456. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2457. cout.width( 8 );.  <br>
  2458. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2459. cout.setFieldWidth( 10 );. <br>
  2460. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2461.  
  2462. </page>
  2463. <page pagename="11.6.4   User-Defined Manipulators">
  2464. <b>11.6.4   User-Defined Manipulators</b><br>
  2465. Users may create their own stream manipulators.  
  2466. <a href="^Code::c:s0p16"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.19</a> shows the creation and use of new stream 
  2467. manipulators <b>bell</b>, <b>ret</b> (carriage return), <b>tab</b>, and 
  2468. <b>endLine</b>. Users may also create their own 
  2469. parameterized stream manipulators--consult your 
  2470. installation's manuals for instructions on how to do this.<br>
  2471.  
  2472. </page>
  2473. </section>
  2474. <section type=Body name=Default title="11.7 Stream Format States">
  2475. <page>
  2476. <font size=18 bold>11.7 Stream Format States</font><hr>
  2477. Various <i>format flags</i> specify the kinds of formatting to 
  2478. be performed during stream I/O operations. The <b>setf</b>, 
  2479. <b>unsetf</b>, and <b>flags</b> member functions control the flag 
  2480. settings. <br>
  2481.  
  2482. </page>
  2483. <page pagename="11.7.1   Format State Flags">
  2484. <b>11.7.1   Format State Flags</b><br>
  2485. Each of the format state flags shown in  <a href="^Illustration::c:s0p4"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 11.20</a> (and 
  2486. some that are not shown) is defined as an enumeration 
  2487. in class <b>ios</b> and is explained in the next several sections.<br>
  2488. <spacer width=16 height=1>These flags can be controlled by the <b>flags</b>, <b>setf</b>, and 
  2489. <b>unsetf</b> member functions, but many C++ programmers 
  2490. prefer to use stream manipulators (see<a href="#s7p17"> Section 11.7.8</a>). 
  2491. The programmer may use the bitwise-or operation, <b>|</b>, to 
  2492. combine various options into a single <b>long</b> value (see 
  2493. <a href="^Code::c:s0p19">Fig.<img src="bckgrnds/icons/code_ico.gif" align=sidebar> 11.23</a>). Calling the <b>flags</b> member function for a 
  2494. stream and specifying these "or-ed" options sets the 
  2495. options on that stream and returns a <b>long</b> value 
  2496. containing the prior options. This value is often saved <br>
  2497.  
  2498. </page>
  2499. <page pagename="11.7.1   Format State Flags">
  2500. so that <b>flags</b> may be called with this saved value to 
  2501. restore the previous stream options.<br>
  2502. <spacer width=16 height=1>The <b>flags</b> function must specify a value representing the 
  2503. settings of all the flags. The one-argument <b>setf</b> function, 
  2504. on the other hand, specifies one or more "or-ed" flags 
  2505. and "ors" them with the existing flag settings to form a 
  2506. new format state. <br>
  2507. The <b>setiosflags</b> parameterized stream manipulator 
  2508. performs the same functions as the <b>setf</b> member 
  2509. function. The <b>resetiosflags</b> stream manipulator 
  2510. performs the same functions as the <b>unsetf</b> member 
  2511. function. To use either of these stream manipulators, be 
  2512. sure to <b>#include <iomanip.h></b>.<br>
  2513.  
  2514. </page>
  2515. <page pagename="11.7.1   Format State Flags">
  2516. The <b>skipws</b> flag indicates that<b> >></b> should skip 
  2517. whitespace on an input stream. The default behavior of 
  2518. <b>>></b> is to skip whitespace. To change this, use the call 
  2519. <b>unsetf(ios::skipws)</b>. The <b>ws</b> stream manipulator also 
  2520. may be used to specify that whitespace should be 
  2521. skipped.<br>
  2522.  
  2523. </page>
  2524. <page pagename="11.7.1   Format State Flags">
  2525. <b>Select the statement that generates a syntax error. </b><br>
  2526. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2527. cout.setf( ios::fixed );.   <br>
  2528. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2529. cout.unsetf( ios::showpoint, ios::left );.  <br>
  2530. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2531. cout.setf( ios::all );. <br>
  2532. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2533. cout.setf( ios::dec );.   <br>
  2534. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2535. cout.unsetf( ios::right, ios::hex );.   <br>
  2536. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2537. cout.setf( ios::scientific );.   <br>
  2538. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2539.  
  2540. </page>
  2541. <page pagename="11.7.2   Trailing Zeros and Decimal Points ">
  2542. (<b>ios::showpoint</b>)
  2543. <b>11.7.2   Trailing Zeros and Decimal Points 
  2544. (<b>ios::showpoint</b>)</b><br>
  2545. The <b>showpoint</b> flag is set to force a floating-point 
  2546. number to be output with its decimal point and trailing 
  2547. zeros. A floating-point value of <b>79.0</b> will print as <b>79</b> 
  2548. without <b>showpoint</b> set and as <b>79.000000</b> (or as many 
  2549. trailing zeros as are specified by the current precision) 
  2550. with <b>showpoint</b> set. The program in  <a href="^Code::c:s0p17"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.21</a> shows 
  2551. the use of the <b>setf</b> member function to set the 
  2552. <b>showpoint</b> flag to control trailing zeros and the printing 
  2553. of the decimal point for floating-point values.<br>
  2554.  
  2555. </page>
  2556. <page pagename="11.7.3   Justification (<b>ios::left</b>, <b>ios::right</b>, ">
  2557. <b>ios::internal</b>)
  2558. <b>11.7.3   Justification (<b>ios::left</b>, <b>ios::right</b>, 
  2559. <b>ios::internal</b>)</b><br>
  2560. The <b>left</b> and <b>right</b> flags enable fields to be left-justified 
  2561. with padding characters to the right, or right-justified 
  2562. with padding characters to the left, respectively. The 
  2563. character to be used for padding is specified by the <b>fill</b> 
  2564. member function or the <b>setfill</b> parameterized stream 
  2565. manipulator (see<a href="#s7p9"> Section 11.7.4</a>). <a href="^Code::c:s0p18"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.22</a> shows 
  2566. the use of the <b>setw</b>, <b>setiosflags</b>, and <b>resetiosflags</b> 
  2567. manipulators and the <b>setf</b> and <b>unsetf</b> member functions 
  2568. to control the left- and right-justification of integer data 
  2569. in a field.<br>
  2570.  
  2571. </page>
  2572. <page pagename="11.7.3   Justification (<b>ios::left</b>, <b>ios::right</b>, ">
  2573. The <b>internal</b> flag indicates that a number's sign (or 
  2574. base when the <b>ios::showbase</b> flag is set; see <a href="#s7p11">Section 
  2575. 11.7.5</a>) should be left-justified within a field, the 
  2576. number's magnitude should be right-justified, and 
  2577. intervening spaces should be padded with the fill 
  2578. character. The <b>left</b>, <b>right</b>, and <b>internal</b> flags are 
  2579. contained in static data member <b>ios::adjustfield</b>. The 
  2580. <b>ios::adjustfield </b>argument must be provided as the 
  2581. second argument to <b>setf</b> when setting the <b>left</b>, <b>right</b>, or 
  2582. <b>internal</b> justification flags. This enables <b>setf</b> to ensure 
  2583. that only one of the three justification flags is set (they 
  2584. are mutually exclusive). <a href="^Code::c:s0p19"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.23</a> shows the use of 
  2585. the <b>setiosflags</b> and <b>setw</b> stream manipulators to specify <br>
  2586.  
  2587. </page>
  2588. <page pagename="11.7.3   Justification (<b>ios::left</b>, <b>ios::right</b>, ">
  2589. internal spacing. Note the use of the <b>ios::showpos</b> flag 
  2590. to force the printing of the plus sign.<br>
  2591.  
  2592. </page>
  2593. <page pagename="11.7.4   Padding (<b>fill</b>, <b>setfill</b>) ">
  2594. <b>11.7.4   Padding (<b>fill</b>, <b>setfill</b>) </b><br>
  2595. The <b><i>fill</i></b> <i>member function </i>specifies the fill character to 
  2596. be used with adjusted fields; if no value is specified, 
  2597. spaces are used for padding. The fill function returns 
  2598. the prior padding character. The <b><i>setfill</i></b> <i>manipulator</i> also 
  2599. sets the padding character.  <a href="^Code::c:s0p20"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 11.24</a> demonstrates 
  2600. the use of the <b>fill</b> member function and the <b>setfill</b> 
  2601. manipulator to control the setting and resetting of the 
  2602. fill character.<br>
  2603.  
  2604. </page>
  2605. <page pagename="11.7.4   Padding (<b>fill</b>, <b>setfill</b>) ">
  2606. <b>Select the statement that produces the output:<p>
  2607. *********************z. </b><br>
  2608. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2609. cout << setw( 21 ) << z;.   <br>
  2610. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2611. cout << setprecision( 22 ) << "**" << z;.  <br>
  2612. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2613. cout << setw( 22 ) << setfill( '*' ) << 'z';. <br>
  2614. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2615. cout << setw( 22 ) << z;.   <br>
  2616. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2617. cout << setfill('z') << setw( 22 ) << '*';.   <br>
  2618. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2619. cout << setiosflags( ios::internal ) << z;.   <br>
  2620. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2621.  
  2622. </page>
  2623. <page pagename="11.7.5   Integral Stream Base (<b>ios::dec</b>, <b>ios::oct</b>, ">
  2624. <b>ios::hex</b>, <b>ios::showbase</b>)
  2625. <b>11.7.5   Integral Stream Base (<b>ios::dec</b>, <b>ios::oct</b>, 
  2626. <b>ios::hex</b>, <b>ios::showbase</b>)</b><br>
  2627. The <b><i>ios::basefield static member</i></b> (used similarly to 
  2628. <b>ios::adjustfield </b>with <b>setf</b>) includes the <b>ios::oct</b>, 
  2629. <b>ios::hex</b>, and <b>ios::dec</b> flag bits to specify that integers 
  2630. are to be treated as octal, hexadecimal, and decimal 
  2631. values, respectively. Stream insertions default to 
  2632. decimal if none of these bits is set. The default for 
  2633. stream extractions is to process the data in the form in 
  2634. which it is supplied--integers starting with 0 are 
  2635. treated as octal values, integers starting with <b>0x</b> or <b>0X 
  2636. </b>are treated as hexadecimal values, and all other integers 
  2637. are treated as decimal values. Once a particular base is <br>
  2638.  
  2639. </page>
  2640. <page pagename="11.7.5   Integral Stream Base (<b>ios::dec</b>, <b>ios::oct</b>, ">
  2641. specified for a stream, all integers on that stream are 
  2642. processed with that base until a new base is specified or 
  2643. until the end of the program.<br>
  2644. <spacer width=16 height=1>Set the <b>showbase</b> flag to force the base of an integral 
  2645. value to be output. Decimal numbers are output 
  2646. normally, octal numbers are output with a leading 0, 
  2647. and hexadecimal numbers are output with either a 
  2648. leading <b>0x</b> or a leading <b>0X</b> (the <b>uppercase</b> flag 
  2649. determines which option is chosen; see <a href="#s7p15">Section 11.7.7</a>).    
  2650. <a href="^Code::c:s0p21">Figure  <img src="bckgrnds/icons/code_ico.gif" align=sidebar>11.25</a> demonstrates the use of the <b>showbase</b> 
  2651. flag to force an integer to print in decimal, octal, and 
  2652. hexadecimal formats.<br>
  2653.  
  2654. </page>
  2655. <page pagename="11.7.6   Floating-Point Numbers; Scientific Notation ">
  2656. (<b>ios::scientific</b>, <b>ios::fixed</b>)
  2657. <b>11.7.6   Floating-Point Numbers; Scientific Notation 
  2658. (<b>ios::scientific</b>, <b>ios::fixed</b>)</b><br>
  2659. The <b><i>ios::scientific flag</i></b> and the <b><i>ios::fixed flag</i></b> are 
  2660. contained in the <i>static data member</i> <b><i>ios::floatfield 
  2661. </i></b>(used similarly to <b>ios::adjustfield </b>and <b>ios::basefield </b>in 
  2662. <b>setf</b>). These flags are used to control the output format 
  2663. of floating-point numbers. The <b>scientific</b> flag is set to 
  2664. force the output of a floating-point number in scientific 
  2665. format. The <b>fixed</b> flag forces a floating-point number to 
  2666. display a specific number of digits (as specified by the 
  2667. <b>precision</b> member function) to the right of the decimal 
  2668. point. Without these flags set, the value of the floating-
  2669. point number determines the output format. <br>
  2670.  
  2671. </page>
  2672. <page pagename="11.7.6   Floating-Point Numbers; Scientific Notation ">
  2673. The call<b> cout.setf(0, ios::floatfield) </b>restores the system 
  2674. default format for outputting floating-point numbers. 
  2675. <a href="^Code::c:s0p22">Figure<img src="bckgrnds/icons/code_ico.gif" align=sidebar> 11.26</a> demonstrates the display of floating-point 
  2676. numbers in fixed and scientific formats using the two 
  2677. argument <b>setf</b> with <b>ios::floatfield</b>.<br>
  2678.  
  2679. </page>
  2680. <page pagename="11.7.7   Uppercase/Lowercase Control ">
  2681. (<b>ios::uppercase</b>)
  2682. <b>11.7.7   Uppercase/Lowercase Control 
  2683. (<b>ios::uppercase</b>)</b><br>
  2684. The <b>ios::uppercase </b>flag is set to force an uppercase <b>X</b> 
  2685. or <b>E</b> to be output with hexadecimal integers or with 
  2686. scientific notation floating-point values, respectively 
  2687. (<a href="^Code::c:s0p23"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.27</a>). When set, the <b>ios::uppercase</b> flag causes 
  2688. all letters in a hexadecimal value to be uppercase.<br>
  2689.  
  2690. </page>
  2691. <page pagename="11.7.7   Uppercase/Lowercase Control ">
  2692. <b>Select the statement that could produce the output:<p>
  2693. 9.99E+04. </b><br>
  2694. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2695. cout << setiosflags( ios::fixed ) << 99900;.   <br>
  2696. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  2697. cout << setiosflags(ios::uppercase) << 9.99e4;. <br>
  2698. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2699. cout << hex << 9.99e4 << endl;.  <br>
  2700. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2701. cout << setiosflags(ios::showpoint) << 9.9E4;.   <br>
  2702. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2703. cout << setiosflags(ios::showpos) << 9.99E4;.   <br>
  2704. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  2705. cout << setiosflags(ios::internal) << 99900;.   <br>
  2706. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2707.  
  2708. </page>
  2709. <page pagename="11.7.8   Setting and Resetting the Format Flags ">
  2710. (<b>flags</b>, <b>setiosflags</b>, <b>resetiosflags</b>)
  2711. <b>11.7.8   Setting and Resetting the Format Flags 
  2712. (<b>flags</b>, <b>setiosflags</b>, <b>resetiosflags</b>)</b><br>
  2713. The <b>flags</b> member function without an argument simply 
  2714. returns (as a <b>long</b> value) the current settings of the 
  2715. format flags. The <b>flags</b> member function with a <b>long</b> 
  2716. argument sets the format flags as specified by the 
  2717. argument and returns the prior flag settings. Any format 
  2718. flags not specified in the argument to <b>flags</b> are reset. 
  2719. Note that the initial settings of the flags on each system 
  2720. may differ. The program of <a href="^Code::c:s0p24"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.28</a> demonstrates the 
  2721. use of the <b>flags</b> member function to set a new format 
  2722. state and save the previous format state, then restore the 
  2723. original format settings.<br>
  2724.  
  2725. </page>
  2726. <page pagename="11.7.8   Setting and Resetting the Format Flags ">
  2727. The <b>setf</b> member function sets the format flags 
  2728. provided in its argument and returns the previous flag 
  2729. settings as a <b>long</b> value as in<br>
  2730. <font size=2><br></font><font size=11><pre>
  2731. long previousFlagSettings = <p>
  2732.          cout.setf( ios::showpoint | ios::showpos );<p>
  2733. </pre></font>
  2734. The <b>setf</b> member function with two <b>long</b> arguments as 
  2735. in<br>
  2736. <font size=2><br></font><font size=11><pre>
  2737. cout.setf( ios::left, ios::adjustfield );<p>
  2738. </pre></font>
  2739. first clears the bits of i<b>os::adjustfield </b>and then sets the 
  2740. <b>ios::left</b> flag. This version of <b>setf</b> is used with the bit 
  2741. fields associated with <b>ios::basefield</b> (represented by 
  2742. <b>ios::dec</b>, <b>ios::oct</b>, and <b>ios::hex</b>), <b>ios::floatfield</b> <br>
  2743.  
  2744. </page>
  2745. <page pagename="11.7.8   Setting and Resetting the Format Flags ">
  2746. (represented by<b> ios::scientific </b>and <b>ios::fixed</b>), and 
  2747. <b>ios::adjustfield</b> (represented by i<b>os::left</b>, <b>ios::right</b>, 
  2748. and <b>ios::internal</b>).<br>
  2749. <spacer width=16 height=1>The <b>unsetf</b> member function resets the designated flags 
  2750. and returns the value of the flags prior to being reset.<br>
  2751.  
  2752. </page>
  2753. </section>
  2754. <section type=Body name=Default title="11.8 Stream Error States">
  2755. <page>
  2756. <font size=18 bold>11.8 Stream Error States</font><hr>
  2757. The state of a stream may be tested through bits in class 
  2758. <b>ios</b>--the base class for the classes <b>istream</b>, <b>ostream</b>, 
  2759. and <b>iostream</b> we are using for I/O. <br>
  2760. <spacer width=16 height=1>The <b>eofbit</b> is set automatically for an input stream when 
  2761. end-of-file is encountered. A program can use the <b>eof</b> 
  2762. member function to determine if end-of-file has been 
  2763. encountered on a stream. The call<br>
  2764. <font size=2><br></font><font size=11><pre>
  2765. cin.eof()<p>
  2766. </pre></font>
  2767. returns true if end-of-file has been encountered on <b>cin</b>, 
  2768. and false otherwise.<br>
  2769.  
  2770. </page>
  2771. <page>
  2772. The <b>failbit</b> is set for a stream when a format error 
  2773. occurs on the stream, but characters have not been lost. 
  2774. The <b>fail</b> member function determines if a stream 
  2775. operation has failed; it is normally possible to recover 
  2776. from such errors.<br>
  2777. <spacer width=16 height=1>The <b>badbit</b> is set for a stream when an error occurs that 
  2778. results in the loss of data. The bad member function 
  2779. determines if a stream operation has failed. Such 
  2780. serious failures are normally nonrecoverable.<br>
  2781. <spacer width=16 height=1>The <b>goodbit</b> is set for a stream if none of the bits <b>eofbit</b>, 
  2782. <b>failbit</b>, or <b>badbit</b> are set for the stream.<br>
  2783.  
  2784. </page>
  2785. <page>
  2786. The <b>good</b> member function returns <b>true</b> if the <b>bad</b>, <b>fail</b> 
  2787. and <b>eof</b> functions would all return false. I/O operations 
  2788. should only be performed on "good" streams.<br>
  2789. <spacer width=16 height=1>The <b>rdstate</b> member function returns the error state of 
  2790. the stream. A call to <b>cout.rdstate</b>, for example, would 
  2791. return the state of the stream which could then be tested 
  2792. by a <b>switch</b> statement that examines<b> ios::eofbit</b>, 
  2793. <b>ios::badbit</b>, <b>ios::failbit</b>, and <b>ios::goodbit</b>. The 
  2794. preferred means of testing the state of a stream is to use 
  2795. the member functions <b>eof</b>, <b>bad</b>, <b>fail</b>, and <b>good</b>--using 
  2796. these functions does not require the programmer to be 
  2797. familiar with particular status bits.<br>
  2798.  
  2799. </page>
  2800. <page>
  2801. The <b>clear</b> member function is normally used to restore 
  2802. a stream's state to "good" so that I/O may proceed on 
  2803. that stream. The default argument for <b>clear</b> is 
  2804. <b>ios::goodbit</b>, so the statement<br>
  2805. <font size=2><br></font><font size=11><pre>
  2806. cin.clear();<p>
  2807. </pre></font>
  2808. clears <b>cin</b> and sets <b>goodbit</b> for the stream. The 
  2809. statement <br>
  2810. <font size=2><br></font><font size=11><pre>
  2811. cin.clear(ios::failbit)<p>
  2812. </pre></font>
  2813. actually sets the <b>failbit</b>. The user might want to do this 
  2814. when performing input on <b>cin</b> with a user-defined type 
  2815. and encountering a problem. The name <b>clear</b> seems 
  2816. inappropriate in this context, but it is correct.<br>
  2817.  
  2818. </page>
  2819. <page>
  2820. The program of <a href="^Code::c:s0p25"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 11.29</a> illustrates the use of the 
  2821. <b>rdstate</b>, <b>eof</b>, <b>fail</b>, <b>bad</b>, <b>good</b>, and <b>clear</b> member 
  2822. functions.<br>
  2823. <spacer width=16 height=1>The<b> operator!</b> member function returns true if either 
  2824. the <b>badbit</b> is set, the <b>failbit</b> is set, or both are set. The 
  2825. <b>operator void* </b>member function returns false if either 
  2826. the <b>badbit</b> is set, the <b>failbit</b> is set, or both are set. These 
  2827. functions are useful in file processing when a true/false 
  2828. condition is being tested in the condition of a selection 
  2829. structure or repetition structure. <br>
  2830.  
  2831. </page>
  2832. <page>
  2833. <b>Drag the correct term to the box associated with the 
  2834. member function:</b><br>
  2835. <component type="drag" width=40 height=18 label="clear" name="clear">   <component type="drag" width=56 height=18 label="rdstate" name="rdstate">   <component type="drag" width=24 height=18 label="eof" name="eof">   <component type="drag" width=32 height=18 label="fail" name="fail">   <component type="drag" width=24 height=18 label="bad" name="bad">   <component type="drag" width=32 height=18 label="good" name="good">   <br>
  2836. Returns the error state of a stream.<component type="drop" width=64 height=18 name="rdstate">  <br>
  2837. Determines if a stream operation has failed.<component type="drop" width=64 height=18 name="bad">  <br>
  2838. Returns true, if fail, eof, and bad all return false.<component type="drop" width=64 height=18 name="good">  <br>
  2839. Determines if end of file has been reached.<component type="drop" width=64 height=18 name="eof">  <br>
  2840. Restores a stream's state to good.<component type="drop" width=64 height=18 name="clear">  <br>
  2841. Determines if the stream operation has failed.<component type="drop" width=64 height=18 name="fail">  <br>
  2842. <component type=button name=b label="Check Your Answer" width=125 height=24>
  2843.  
  2844. </page>
  2845. </section>
  2846. <section type=Body name=Default title="11.9 Tying an Output Stream to an Input Stream">
  2847. <page>
  2848. <font size=18 bold>11.9 Tying an Output Stream to an Input 
  2849. Stream</font><hr>
  2850. Interactive applications generally involve an <b>istream</b> 
  2851. for input and an <b>ostream</b> for output. When a prompting 
  2852. message appears on the screen, the user responds by 
  2853. entering the appropriate data. Obviously, the prompt 
  2854. needs to appear before the input operation proceeds. 
  2855. With output buffering, outputs appear only when the 
  2856. buffer fills, when outputs are flushed explicitly by the 
  2857. program or automatically at the end of the program. 
  2858. C++ provides member function <b>tie</b> to synchronize (i.e., 
  2859. "tie together") the operation of an <b>istream</b> and an <br>
  2860.  
  2861. </page>
  2862. <page>
  2863. <b>ostream</b> to ensure that outputs appear before their 
  2864. subsequent inputs. The call<br>
  2865. <font size=2><br></font><font size=11><pre>
  2866. cin.tie( &cout );<p>
  2867. </pre></font>
  2868. ties <b>cout (an ostream)</b> to <b>cin (an istream)</b>. Actually, 
  2869. this particular call is redundant because C++ performs 
  2870. this operation automatically to create a user's standard 
  2871. input/output environment. The user would, however, 
  2872. explicitly tie together other <b>istream/ostream</b> pairs. To 
  2873. untie an input stream, <b>inputStream</b>, from an output 
  2874. stream, use the call<br>
  2875. <font size=2><br></font><font size=11><pre>
  2876. inputStream.tie( 0 );<p>
  2877. </pre></font>
  2878.  
  2879. </page>
  2880. </section>
  2881. <section type=Body name=Default title="11.10 Summary">
  2882. <page>
  2883. <font size=18 bold>11.10 Summary</font><hr>
  2884. <indent width=8 delay>*   I/O operations are performed in a manner sensitive to 
  2885. the type of the data. </indent>
  2886. <indent width=8 delay>*   C++ I/O occurs in streams of bytes. A stream is simply a sequence of bytes. </indent>
  2887. <indent width=8 delay>*   I/O mechanisms of the system move bytes from 
  2888. devices to memory and vice versa in an efficient and 
  2889. reliable manner. </indent>
  2890. <indent width=8 delay>*  C++ provides "low-level" and "high-level" I/O capabilities. Low-level I/O-capabilities specify that some 
  2891. number of bytes should be transferred device-to-memory or memory-to-device. High-level I/O is performed </indent>
  2892.  
  2893. </page>
  2894. <page>
  2895. <indent width=8 delay>*   with bytes grouped into meaningful units such as integers, floats, characters, strings and user-defined types.</indent>
  2896. <indent width=8 delay>*   C++ provides both unformatted I/O and formatted I/
  2897. O operations. Unformatted I/O transfers are fast, but 
  2898. process raw data that is difficult for people to use. Formatted I/O processes data in meaningful units, but 
  2899. requires extra processing time that can negatively 
  2900. impact high-volume data transfers. </indent>
  2901. <indent width=8 delay>*   Most C++ programs include the <b><iostream.h></b> 
  2902. header file that contains basic information required for 
  2903. all stream I/O operations. </indent>
  2904. <indent width=8 delay>*  The <b><iomanip.h></b> header contains information for 
  2905. formatted input/output with parameterized stream </indent>
  2906.  
  2907. </page>
  2908. <page>
  2909. <indent width=8 delay>*   manipulators.</indent>
  2910. <indent width=8 delay>*   The <b><fstream.h></b> header contains information for 
  2911. file processing operations.</indent>
  2912. <indent width=8 delay>*   The <b>istream</b> class supports stream input operations. </indent>
  2913. <indent width=8 delay>*   The <b>ostream</b> class supports stream output operations. </indent>
  2914. <indent width=8 delay>*   The <b>iostream</b> class supports both stream-input and 
  2915. stream-output operations. </indent>
  2916. <indent width=8 delay>*   The <b>istream</b> class and the <b>ostream</b> class are each 
  2917. derived through single inheritance from the <b>ios</b> base 
  2918. class. </indent>
  2919. <indent width=8 delay>*  The <b>iostream</b> class is derived through multiple inheritance from both the <b>istream</b> class and the <b>ostream</b> </indent>
  2920.  
  2921. </page>
  2922. <page>
  2923. <indent width=8 delay>*   class. </indent>
  2924. <indent width=8 delay>*   The left shift operator (<b><<</b>) is overloaded to designate 
  2925. stream output and is referred to as the stream-insertion 
  2926. operator. </indent>
  2927. <indent width=8 delay>*   The right shift operator (<b>>></b>) is overloaded to designate stream input and is referred to as the stream-extraction operator. </indent>
  2928. <indent width=8 delay>*   The <b>istream</b> object <b>cin</b> is tied to the standard input 
  2929. device, normally the keyboard. </indent>
  2930. <indent width=8 delay>*   The <b>ostream</b> class object <b>cout</b> is tied to the standard 
  2931. output device, normally the display screen. </indent>
  2932. <indent width=8 delay>*  The <b>ostream</b> class object <b>cerr</b> is tied to the standard 
  2933. error device. Outputs to <b>cerr</b> are unbuffered; each </indent>
  2934.  
  2935. </page>
  2936. <page>
  2937. <indent width=8 delay>*   insertion to <b>cerr</b> appears immediately. </indent>
  2938. <indent width=8 delay>*   Stream manipulator <b>endl</b> issues a newline character 
  2939. and flushes the output buffer. </indent>
  2940. <indent width=8 delay>*   The C++ compiler determines data types automatically for input and output. </indent>
  2941. <indent width=8 delay>*   Addresses are displayed in hexadecimal format by 
  2942. default. </indent>
  2943. <indent width=8 delay>*   To print the address in a pointer variable, cast the 
  2944. pointer to <b>void*</b>. </indent>
  2945. <indent width=8 delay>*   Member function put outputs one character. Calls to 
  2946. put may be cascaded. </indent>
  2947. <indent width=8 delay>*  Stream input is performed with the stream-extraction 
  2948. operator <b>>></b>. This operator automatically skips </indent>
  2949.  
  2950. </page>
  2951. <page>
  2952. <indent width=8 delay>*   whitespace characters in the input stream. </indent>
  2953. <indent width=8 delay>*   The <b>>></b> operator returns false when end-of-file is 
  2954. encountered on a stream. </indent>
  2955. <indent width=8 delay>*   Stream extraction causes <b>failbit</b> to be set for 
  2956. improper input, and <b>badbit</b> to be set if the operation 
  2957. fails.</indent>
  2958. <indent width=8 delay>*   A series of values can be input using the stream-
  2959. extraction operation in a <b>while</b> loop header. The extraction returns 0 when end-of-file is encountered. </indent>
  2960. <indent width=8 delay>*   The <b>get</b> member function with no arguments inputs 
  2961. one character and returns the character; <b>EOF</b> is 
  2962. returned if end-of-file is encountered on the stream. </indent>
  2963. <indent width=8 delay>*  The <b>get</b> member function with an argument of type </indent>
  2964.  
  2965. </page>
  2966. <page>
  2967. <indent width=8 delay>*   <b>char</b> inputs one character. <b>EOF</b> is returned when end-
  2968. of-file is encountered; otherwise, the <b>istream</b> object for 
  2969. which the <b>get</b> member function is being invoked is 
  2970. returned.</indent>
  2971. <indent width=8 delay>*   The <b>get</b> member function with three arguments--a 
  2972. character array, a size limit, and a delimiter (with 
  2973. default value newline)--reads characters from the input 
  2974. stream up to a maximum of limit - 1 characters and terminates, or terminates when the delimiter is read. The 
  2975. input string is terminated with a null character. The 
  2976. delimiter is not placed in the character array, but 
  2977. remains in the input stream.</indent>
  2978. <indent width=8 delay>*  The <b>getline</b> member function operates like the three </indent>
  2979.  
  2980. </page>
  2981. <page>
  2982. <indent width=8 delay>*   argument <b>get</b> member function. The <b>getline</b> function 
  2983. removes the delimiter from the input stream, but does 
  2984. not store it in the string.</indent>
  2985. <indent width=8 delay>*   Member function <b>ignore</b> skips the specified number 
  2986. of characters (default is one character) in the input 
  2987. stream; it terminates if the specified delimiter is 
  2988. encountered (the default delimiter is <b>EOF</b>).</indent>
  2989. <indent width=8 delay>*   The <b>putback</b> member function places the previous 
  2990. character obtained by a <b>get</b> on a stream back onto that 
  2991. stream. </indent>
  2992. <indent width=8 delay>*   The <b>peek</b> member function returns the next character 
  2993. from an input stream, but does not remove the character 
  2994. from the stream.</indent>
  2995.  
  2996. </page>
  2997. <page>
  2998. <indent width=8 delay>*   C++ offers type-safe input/output. If unexpected data 
  2999. is processed by the <b><<</b> and <b>>></b> operators, various error 
  3000. flags are set which the user may test to determine if an 
  3001. I/O operation succeeded or failed. </indent>
  3002. <indent width=8 delay>*   Unformatted I/O is performed with member functions <b>read</b> and <b>write</b>. These input or output some number of bytes to or from memory beginning at a 
  3003. designated memory address. They are input or output as 
  3004. raw bytes with no formatting. </indent>
  3005. <indent width=8 delay>*   The <b>gcount</b> member function returns the number of 
  3006. characters input by the previous read operation on that 
  3007. stream.</indent>
  3008. <indent width=8 delay>*  Member function read inputs a specified number of </indent>
  3009.  
  3010. </page>
  3011. <page>
  3012. <indent width=8 delay>*   characters into a character array. <b>failbit</b> is set if fewer 
  3013. than the specified number of characters are read.</indent>
  3014. <indent width=8 delay>*   To change the base in which integers output, use the 
  3015. manipulator <b>hex</b> to set the base to hexadecimal (base 
  3016. 16) or <b>oct</b> to set the base to octal (base 8). Use manipulator <b>dec</b> to reset the base to decimal. The base remains 
  3017. the same until explicitly changed. </indent>
  3018. <indent width=8 delay>*   The parameterized stream manipulator <b>setbase</b> also 
  3019. sets the base for integer output. <b>setbase</b> takes one integer argument of <b>10</b>, <b>8</b>, or <b>16</b> to set the base. </indent>
  3020. <indent width=8 delay>*  Floating-point precision can be controlled using 
  3021. either the <b>setprecision</b> stream manipulator or the <b>precision</b> member function. Both set the precision for all </indent>
  3022.  
  3023. </page>
  3024. <page>
  3025. <indent width=8 delay>*   subsequent output operations until the next precision-
  3026. setting call. The <b>precision</b> member function with no 
  3027. argument returns the current precision value. </indent>
  3028. <indent width=8 delay>*   Parameterized manipulators require the inclusion of 
  3029. the <b><iomanip.h></b> header file. </indent>
  3030. <indent width=8 delay>*  Member function <b>width</b> sets the field width and 
  3031. returns the previous width. Values smaller than the field 
  3032. are padded with fill characters. The field width setting 
  3033. applies only for the next insertion or extraction; the 
  3034. field width is implicitly set to <b>0</b> afterward (subsequent 
  3035. values will be output as large as they need to be). Values larger than a field are printed in their entirety. Function <b>width</b> with no argument returns the current width </indent>
  3036.  
  3037. </page>
  3038. <page>
  3039. <indent width=8 delay>*   setting. Manipulator <b>setw</b> also sets the width.</indent>
  3040. <indent width=8 delay>*   For input, the <b>setw</b> stream manipulator establishes a 
  3041. maximum string size; if a larger string is entered, the 
  3042. larger line is broken into pieces no larger than the designated size.</indent>
  3043. <indent width=8 delay>*   Users may create their own stream manipulators.</indent>
  3044. <indent width=8 delay>*   Member functions <b>setf</b>, <b>unsetf</b>, and <b>flags</b> control the 
  3045. flag settings.</indent>
  3046. <indent width=8 delay>*   The <b>skipws</b> flag indicates that <b>>></b> should skip 
  3047. whitespace on an input stream. The <b>ws</b> stream manipulator also skips over leading whitespace in an input 
  3048. stream.</indent>
  3049. <br>
  3050.  
  3051. </page>
  3052. <page>
  3053. <indent width=8 delay>*   Format flags are defined as an enumeration in class 
  3054. <b>ios</b>.</indent>
  3055. <indent width=8 delay>*   Format flags are controlled by the <b>flags</b> and <b>setf</b> 
  3056. member functions, but many C++ programmers prefer 
  3057. to use stream manipulators. The bitwise-or operation, <b>|</b>, 
  3058. can be used to combine various options into a single 
  3059. <b>long</b> value. Calling the <b>flags</b> member function for a 
  3060. stream and specifying these "or-ed" options sets the 
  3061. options on that stream and returns a <b>long</b> value containing the prior options. This value is often saved so <b>flags</b> 
  3062. may be called with this saved value to restore the previous stream options.</indent>
  3063. <indent width=8 delay>*  The <b>flags</b> function must specify a value representing </indent>
  3064.  
  3065. </page>
  3066. <page>
  3067. <indent width=8 delay>*   the total settings of all the flags. The <b>setf</b> function with 
  3068. one argument, on the other hand, automatically "ors" 
  3069. the specified flags with the existing flag settings to 
  3070. form a new format state. </indent>
  3071. <indent width=8 delay>*   The <b>showpoint</b> flag is set to force a floating-point 
  3072. number to be output with a decimal point and number 
  3073. of significant digits specified by the precision. </indent>
  3074. <indent width=8 delay>*   The <b>left</b> and <b>right</b> flags cause fields to be left-justified with padding characters to the right, or right-justified with padding characters to the left.</indent>
  3075. <indent width=8 delay>*  The <b>internal</b> flag indicates that a number's sign (or 
  3076. base when the flag <b>ios::showbase</b> is set) should be left-
  3077. justified within a field, magnitude should be right-justi</indent>
  3078.  
  3079. </page>
  3080. <page>
  3081. <indent width=8 delay>*   fied, and intervening spaces should be padded with the 
  3082. fill character. </indent>
  3083. <indent width=8 delay>*   i<b>os::adjustfield</b> contains the flags <b>left</b>, <b>right</b>, and 
  3084. <b>internal</b>. </indent>
  3085. <indent width=8 delay>*   Member function <b>fill</b> specifies the fill character to be 
  3086. used with <b>left</b>, <b>right</b>, and <b>internal</b> adjusted fields 
  3087. (space is the default); the prior padding character is 
  3088. returned. Stream manipulator <b>setfill</b> also sets the fill 
  3089. character. </indent>
  3090. <indent width=8 delay>*  Static member i<b>os::basefield</b> includes the <b>oct</b>, <b>hex</b>, 
  3091. and <b>dec</b> bits to specify that integers are to be treated as 
  3092. octal, hexadecimal, and decimal values, respectively. 
  3093. Integer output defaults to decimal if none of these bits </indent>
  3094.  
  3095. </page>
  3096. <page>
  3097. <indent width=8 delay>*   is set; stream extractions process the data in the form in 
  3098. which it is supplied.</indent>
  3099. <indent width=8 delay>*   Set the <b>showbase</b> flag to force the base of an integral 
  3100. value to be output. </indent>
  3101. <indent width=8 delay>*   Static data member i<b>os::floatfield</b> contains the flags 
  3102. <b>scientific</b> and the <b>fixed</b>. Set the <b>scientific</b> flag to output 
  3103. a floating-point number in scientific format. Set the 
  3104. <b>fixed</b> flag to output a floating-point number with the 
  3105. precision specified by the <b>precision</b> member function. </indent>
  3106. <indent width=8 delay>*   The call <b>cout.setf(0, ios::floatfield) </b>restores the 
  3107. default format for displaying floating-point numbers. </indent>
  3108. <indent width=8 delay>*  Set the <b>uppercase</b> flag to force an uppercase <b>X</b> or<b> E</b> 
  3109. to be output with hexadecimal integers or with scien</indent>
  3110.  
  3111. </page>
  3112. <page>
  3113. <indent width=8 delay>*   tific notation floating-point values, respectively. When 
  3114. set, the <b>ios::uppercase</b> flag causes all letters in a hexadecimal value to be uppercase.</indent>
  3115. <indent width=8 delay>*   Member function <b>flags</b> with no argument returns the 
  3116. <b>long</b> value of the current settings of the format flags. 
  3117. Member function <b>flags</b> with a <b>long</b> argument sets the 
  3118. format flags specified by the argument and returns the 
  3119. prior flag settings. </indent>
  3120. <indent width=8 delay>*   Member function <b>setf</b> sets the format flags in its 
  3121. argument and returns the previous flag settings as a 
  3122. <b>long</b> value.</indent>
  3123. <indent width=8 delay>*  Member function <b>setf(long setBits, long resetBits)</b> 
  3124. clears the bits of the <b>resetBits</b>, then sets the bit in <b>set</b></indent>
  3125.  
  3126. </page>
  3127. <page>
  3128. <indent width=8 delay>*   <b>Bits</b>.</indent>
  3129. <indent width=8 delay>*   Member function <b>unsetf</b> resets the designated flags 
  3130. and returns the value of the flags prior to being reset.</indent>
  3131. <indent width=8 delay>*   Parameterized stream manipulator <b>setiosflags</b> performs the same functions as member function <b>flags</b>. </indent>
  3132. <indent width=8 delay>*   Parameterized stream manipulator <b>resetiosflags</b> performs the same functions as member function <b>unsetf</b>. </indent>
  3133. <indent width=8 delay>*   The state of a stream may be tested through bits in 
  3134. class <b>ios</b>. </indent>
  3135. <indent width=8 delay>*   The <b>eofbit</b> is set for an input stream when end-of-file 
  3136. is encountered during an input operation. The <b>eof</b> member function is used to determine if the <b>eofbit</b> has been 
  3137. set.</indent>
  3138.  
  3139. </page>
  3140. <page>
  3141. <indent width=8 delay>*   The <b>failbit</b> is set for a stream when a format error 
  3142. occurred on the stream, but characters have not been 
  3143. lost. The <b>fail</b> member function determines if a stream 
  3144. operation has failed; it is normally possible to recover 
  3145. from such errors.</indent>
  3146. <indent width=8 delay>*   The <b>badbit</b> is set for a stream when an error occurred 
  3147. that resulted in the loss of data. The <b>bad</b> member function determines if a stream operation has failed. Such 
  3148. serious failures are normally nonrecoverable.</indent>
  3149. <indent width=8 delay>*   The <b>good</b> member function returns true if the bad, 
  3150. <b>fail</b>, and <b>eof</b> functions would all return false. I/O operations should only be performed on "good" streams.</indent>
  3151. <indent width=8 delay>*  The <b>rdstate</b> member function returns the error state </indent>
  3152.  
  3153. </page>
  3154. <page>
  3155. <indent width=8 delay>*   of the stream. </indent>
  3156. <indent width=8 delay>*   Member function <b>clear</b> is normally used to restore a 
  3157. stream's state to "good" so that I/O may proceed on that 
  3158. stream.</indent>
  3159. <indent width=8 delay>*   C++ provides the tie member function to synchronize <b>istream</b> and <b>ostream</b> operations to ensure that outputs appear before subsequent inputs. </indent>
  3160.  
  3161. </page>
  3162. </section>
  3163.  
  3164. <section type=Popup name=Debug title="Testing">
  3165. <page>
  3166. This chapter does not contain any Testing and Debugging tips.
  3167. </page>
  3168. </section>
  3169. <section type=Popup name=AppletPopup title="Applet Examples">
  3170. <page>
  3171. This chapter does not contain any Applet Examples.
  3172. </page>
  3173. </section>
  3174. </chapter>
  3175. </html>
  3176. </html>
  3177.